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:
- Using SelectedItem
- Adding UpdateSourceTrigger=PropertyChanged
- IsSynchronizedWithCurrentItem="True" to be sure Selected item always synchronized with actual value
- 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:
- https://stackoverflow.com/a/4902454/2758833
- https://stackoverflow.com/a/2883923/2758833
- SelectedValuePath Documentation:
- 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:
- https://stackoverflow.com/a/247482/2758833