WPF组合框与List< string>的绑定 [英] 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;}
我也在XAML中定义了一个组合框
I also have a Combobox defined in XAML as such
<Combobox ItemsSource="{Binding Property , Mode="TwoWay"}" Text="Select Option" />
现在,组合框正确显示了两个选项:"string1"和"string2"
Now the combobox correctly displays two options :"string1" and "string2"
当用户选择一个或另一个时,我想用该值设置 SimpleStringProperty
.但是,通过双向绑定从组合框返回的值"不是selectedItem,而是 List< String>
.我该怎么做呢?我是wpf的新手,所以请原谅我的业余爱好.
When the user selects one or the other, I want to set SimpleStringProperty
with that value. However, the 'value' im getting back from the combobox through the two way binding is not the selectedItem, but the List<String>
. How can I do this right? I'm fairly new to wpf, so please excuse the amateurism.
推荐答案
<Combobox ItemsSource="{Binding Property}" SelectedItem="{Binding SimpleStringProperty, Mode=TwoWay}" Text="Select Option" />
这未经测试,但至少应该与您所需要的非常接近.
That's untested, but it should at least be pretty close to what you need.
这篇关于WPF组合框与List< string>的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!