[英]WPF Combobox binding with List<string>
I have two properties, one which is a list of string and the other just a string. 我有两个属性,一个是字符串列表,另一个是字符串。
private List<String> _property;
public List<String> Property
get
{
return new List<string>(){"string1", "string2"};
}
set{_property = value
}
public String SimpleStringProperty{get;set;}
I also have a Combobox defined in XAML as such 我也有一个在XAML中定义的Combobox
<Combobox ItemsSource="{Binding Property , Mode="TwoWay"}" Text="Select Option" />
Now the combobox correctly displays two options :"string1" and "string2" 现在组合框正确显示两个选项:“string1”和“string2”
When the user selects one or the other, I want to set SimpleStringProperty
with that value. 当用户选择一个或另一个时,我想用该值设置
SimpleStringProperty
。 However, the 'value' im getting back from the combobox through the two way binding is not the selectedItem, but the List<String>
. 但是,通过双向绑定从组合框中返回的“值”不是selectedItem,而是
List<String>
。 How can I do this right? 我怎么能这样做? I'm fairly new to wpf, so please excuse the amateurism.
我对wpf很新,所以请原谅业余爱好者。
<Combobox ItemsSource="{Binding Property}" SelectedItem="{Binding SimpleStringProperty, Mode=TwoWay}" Text="Select Option" />
这是未经测试的,但它至少应该非常接近你需要的。
You need to bind to the String property using the SelectedItem
property of the combobox
. 您需要使用
combobox
的SelectedItem
属性绑定到String属性。
<Combobox ItemsSource="{Binding Property}"
SelectedItem="{Binding SimpleStringProperty}"
IsSynchronizedWithCurrentItem="True"
Text="Select Option" />
What helped me: 是什么帮助了我:
So at the end best way, if source is 所以最好的方式,如果来源是
List<string>
Example: 例:
<ComboBox
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding SomeBindingPropertyList}"
SelectedItem="{Binding SomeBindingPropertySelectedCurrently,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
Additional Info 附加信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.