Bind text to selected item in combobox

♀尐吖头ヾ 提交于 2019-12-22 17:43:30

问题


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:

  • SelectedItem
  • SelectedValue
  • SelectedValuePath
  • DisplayMemberPath

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!