是否可以将ComboBox ItemsSource绑定到只读字符串[]?

时间:2012-03-16 18:05:35

标签: wpf combobox bind readonly itemssource

我的WPF项目中有一个组合框,我想在我的配置类中用readonly字符串数组定义它。这样我就可以很容易地重新配置组合框项目。

是否可以将我的ItemsSource属性绑定到只读字符串[]?怎么做?

2 个答案:

答案 0 :(得分:3)

MainWindow:

<Window x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication4"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <ComboBox ItemsSource="{Binding List, Source={x:Static local:Configuration.Instance}}"></ComboBox>
</StackPanel>

配置文件:

public class Configuration
{

    // Singleton              
    private static Configuration _instance;
    public static Configuration Instance
    {
        get
        {
            if (_instance == null)
                _instance = new Configuration();

            return _instance;
        }
    }

    public IEnumerable<string> List
    {
        get
        {
            return new List<string>()
            {
                "toto 1",
                "toto 2"
            };
        }
    }

    public Configuration()
    {

    }
}

答案 1 :(得分:3)

是,复制/粘贴/编译以下内容:

<ComboBox ItemsSource="Is it possible to Bind a ComboBox (WPF) ItemsSource to a read only string[]"/>