WPF Combobox binding with List<string>

<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.


You need to bind to the String property using the SelectedItem property of the combobox.

<Combobox ItemsSource="{Binding Property}" 
          SelectedItem="{Binding SimpleStringProperty}" 
          IsSynchronizedWithCurrentItem="True" 
          Text="Select Option" />

What helped me:

  1. Using SelectedItem
  2. Adding UpdateSourceTrigger=PropertyChanged
  3. IsSynchronizedWithCurrentItem="True" to be sure Selected item always synchronized with actual value
  4. Mode=TwoWay if you need to update as from source as from GUI

So at the end best way, if source is

List<string>

Example:

 <ComboBox 
    IsSynchronizedWithCurrentItem="True"
    ItemsSource="{Binding SomeBindingPropertyList}"
    SelectedItem="{Binding SomeBindingPropertySelectedCurrently, 
                    Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

Additional Info

  • Difference between SelectedValue and SelectedItem:
    1. https://stackoverflow.com/a/4902454/2758833
    2. https://stackoverflow.com/a/2883923/2758833
  • SelectedValuePath Documentation:
    1. https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.primitives.selector.selectedvaluepath
  • SelectedValue updates possible bugs for .NET 4 and .NET 4.5:
    1. https://stackoverflow.com/a/247482/2758833

Tags:

C#

Wpf