C# WPF组合框与列表绑定<;字符串>;
我有两个属性,一个是字符串列表,另一个只是字符串C# WPF组合框与列表绑定<;字符串>;,c#,wpf,C#,Wpf,我有两个属性,一个是字符串列表,另一个只是字符串 private List<String> _property; public List<String> Property get { return new List<string>(){"string1", "string2"}; } set{_property = value } public String SimpleStringProperty{get;set;} private List\u属性;
private List<String> _property;
public List<String> Property
get
{
return new List<string>(){"string1", "string2"};
}
set{_property = value
}
public String SimpleStringProperty{get;set;}
private List\u属性;
公共列表属性
得到
{
返回新列表(){“string1”、“string2”};
}
设置{u属性=值
}
公共字符串SimpleStringProperty{get;set;}
我也在XAML中定义了一个组合框
<Combobox ItemsSource="{Binding Property , Mode="TwoWay"}" Text="Select Option" />
现在,组合框正确显示两个选项:“string1”和“string2”
当用户选择其中一个时,我想用该值设置
SimpleStringProperty
。但是,通过双向绑定从组合框返回的“值”不是selectedItem,而是列表
。我怎样才能做到这一点?我是wpf的新手,所以请原谅我的业余爱好 您需要使用组合框的SelectedItem
属性绑定到字符串属性
<Combobox ItemsSource="{Binding Property}"
SelectedItem="{Binding SimpleStringProperty}"
IsSynchronizedWithCurrentItem="True"
Text="Select Option" />
这是未经测试的,但至少应该非常接近您的需要。是什么帮助了我:
List<string>
列表
示例:
<ComboBox
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding SomeBindingPropertyList}"
SelectedItem="{Binding SomeBindingPropertySelectedCurrently,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
其他信息
- SelectedValue和SelectedItem之间的差异:
- SelectedValuePath文档:
- SelectedValue更新.NET 4和.NET 4.5的可能错误:
SelectedValue
而不设置SelectedValuePath
。您链接到的问题是有人试图在未设置SelectedValuePath
的情况下使用SelectedValue
。这个问题的公认答案完全无法理解为什么SelectedValue
不起作用。人们不阅读文档。我建议您阅读文档,而不是在StackOverflow上发布错误信息。。这对我来说很有效,但我缺少了:UpdateSourceTrigger=PropertyChanged
<ComboBox
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding SomeBindingPropertyList}"
SelectedItem="{Binding SomeBindingPropertySelectedCurrently,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />