问题
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.
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:
<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.
回答1:
The Wpf ComboBox has three selection properties and one display property:
SelectedItemSelectedValueSelectedValuePathDisplayMemberPath
When using SelectedValue you should also set the SelectedValuePath (almost always). Understand that the Items in your case contains a sequence (ItemCollection) of ComboBoxItem objects, 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.Content property (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 SelctedValue to the MyProperty property using the selected item's Content property, which happens to be the strings you are looking for.
回答2:
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:
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.
来源:https://stackoverflow.com/questions/23222778/bind-text-to-selected-item-in-combobox