wpf 将文本绑定到组合框中的选定项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23222778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bind text to selected item in combobox
提问by A Torres
I have a text field on the database, but now I don't want it to be a free open field, I want to restrict it to: let's say A, B and C.
我在数据库上有一个文本字段,但现在我不希望它是一个自由开放的字段,我想将其限制为:假设 A、B 和 C。
For this I want to use a Combobox.
为此,我想使用组合框。
Question: How do I bind the selected item to the string property, given that the Items of the combobox are defined in XAML?
问题:鉴于组合框的项目是在 XAML 中定义的,如何将所选项目绑定到字符串属性?
XAML:
XAML:
<ComboBox SelectedValue="{Binding Path=MyProperty}"> <!-- Not working-->
<ComboBoxItem>A</ComboBoxItem>
<ComboBoxItem>B</ComboBoxItem>
<ComboBoxItem>C</ComboBoxItem>
</ComboBox>
Class:
班级:
public Class MyClass:INotifyPropertyChanged
{
private string myProperty;
public string MyProperty
{
get{return myProperty;}
set{
myProperty=value;
OnPropertyChanged("MyProperty");
}
}
}
So, the user will change the selected item, and the new value will be updated on the databound object.
因此,用户将更改所选项目,新值将在数据绑定对象上更新。
EDIT: Thanks to the comments and answers I partially solved the problem, the only issue was that the combobox selection was empty when the program started. I solved it like this:
编辑:感谢评论和答案,我部分解决了问题,唯一的问题是程序启动时组合框选择为空。我是这样解决的:
<ComboBox SelectedValuePath="Content">
<ComboBoxItem>A</ComboBoxItem>
<ComboBoxItem>B</ComboBoxItem>
<ComboBoxItem>C</ComboBoxItem>
<ComboBox.SelectedValue>
<Binding Path="MyProperty" Mode="TwoWay"/>
</ComboBox.SelectedValue>
</ComboBox>
I moved the selected value part out of the attributes of the Combobox, and used property element sintax, this ensures that the collection is defined before it is used.
我将选定的值部分移出了 Combobox 的属性,并使用了属性元素 sintax,这确保了集合在使用之前已定义。
回答by m-y
The Wpf ComboBoxhas three selection properties and one display property:
WpfComboBox具有三个选择属性和一个显示属性:
SelectedItemSelectedValueSelectedValuePathDisplayMemberPath
SelectedItemSelectedValueSelectedValuePathDisplayMemberPath
When using SelectedValueyou should also set the SelectedValuePath(almost always). Understand that the Itemsin your case contains a sequence (ItemCollection) of ComboBoxItemobjects, and just like any other object you must specify the SelectedValuePath(read property) that you want to bind to; In this case, you want to access the ComboBoxItem.Contentproperty (http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.content(v=vs.110).aspx).
使用时,SelectedValue您还应该设置SelectedValuePath(几乎总是)。了解Items在您的情况下包含对象序列 ( ItemCollection) ComboBoxItem,并且就像任何其他对象一样,您必须指定SelectedValuePath要绑定到的(读取属性);在这种情况下,您想要访问该ComboBoxItem.Content属性 ( http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.content(v=vs.110).aspx)。
<ComboBox SelectedValue="{Binding Path=MyProperty}" SelectedValuePath="Content">
<ComboBoxItem>A</ComboBoxItem>
<ComboBoxItem>B</ComboBoxItem>
<ComboBoxItem>C</ComboBoxItem>
</ComboBox>
Now you are binding the SelctedValueto the MyPropertyproperty using the selected item's Contentproperty, which happens to be the strings you are looking for.
现在您正在使用所选项目的属性将 绑定SelctedValue到属性,这恰好是您要查找的字符串。MyPropertyContent
回答by TMan
I don't like hard coding combobox items. In this situation being so simple I'd have a list of that type to the itemsource of the combo box. If it's more complicated than an List of strings, I'd make a lookup table in your database and use that instead. For the String list route it would be something like:
我不喜欢硬编码组合框项目。在这种情况如此简单的情况下,我会在组合框的 itemsource 中有一个该类型的列表。如果它比字符串列表更复杂,我会在您的数据库中创建一个查找表并使用它。对于字符串列表路由,它将类似于:
public List<String> LetterTypes { get; set; }
public String SelectedLetterType { get; set; }
int ctor:
国际贸易商:
LetterTypes = new List<String> { A, B, C }
Then in your View:
然后在您的视图中:
<ComboBox ItemsSource="{Binding LetterTypes}" SelectedItem="{Binding SelectedLetterType}" />
I see you already found a fix for your problem but maybe for future reference, this may be another way to populate a combobox.
我看到您已经找到了解决问题的方法,但也许为了将来参考,这可能是填充组合框的另一种方法。

