我在我的WPF项目中有一个组合框,我想让它在我的配置类中由只读字符串数组定义的项目。这样我可以很容易地重新配置组合框项目。是否可以将ComboBox ItemsSource绑定到只读字符串[]?
是否有可能将我的ItemsSource属性绑定到只读字符串[]?如何做到这一点?
我在我的WPF项目中有一个组合框,我想让它在我的配置类中由只读字符串数组定义的项目。这样我可以很容易地重新配置组合框项目。是否可以将ComboBox ItemsSource绑定到只读字符串[]?
是否有可能将我的ItemsSource属性绑定到只读字符串[]?如何做到这一点?
主窗口:
<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()
{
}
}
是,复制/粘贴/编译如下:
<ComboBox ItemsSource="Is it possible to Bind a ComboBox (WPF) ItemsSource to a read only string[]"/>
这是可能的。你有什么尝试? – Bernard 2012-03-16 18:08:48
当然,我已经尝试过,但是我做不到。这就是为什么我想在这里得到一些帮助。你能发布代码吗? – jpnavarini 2012-03-16 18:11:09