20

I have two properties, one which is a list of string and the other just a string.

private List<String> _property;
public List<String> Property
get
{
return new List<string>(){"string1", "string2"};
}
set{_property = value
}

public String SimpleStringProperty{get;set;}

I also have a Combobox defined in XAML as such

<Combobox ItemsSource="{Binding Property , Mode="TwoWay"}" Text="Select Option" />    

Now the combobox correctly displays two options :"string1" and "string2"

When the user selects one or the other, I want to set SimpleStringProperty with that value. However, the 'value' im getting back from the combobox through the two way binding is not the selectedItem, but the List<String>. How can I do this right? I'm fairly new to wpf, so please excuse the amateurism.

mo alaz
  • 4,529
  • 8
  • 30
  • 36

3 Answers3

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

Chris
  • 971
  • 7
  • 15
3

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" />
d.moncada
  • 16,900
  • 5
  • 53
  • 82
2

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

MHN
  • 103
  • 2
  • 7
Ony
  • 399
  • 6
  • 16
  • I recommend reading the documentation on ComboBox.SelectedValue instead of just guessing about it. – 15ee8f99-57ff-4f92-890c-b56153 Jun 16 '16 at 03:44
  • That's the kind of thing I was referring to. Some people try to use `SelectedValue` without setting `SelectedValuePath`. The question you linked to is *an example* of somebody trying to use `SelectedValue` without setting `SelectedValuePath`. The accepted answer to that question totally failed to understand *why* `SelectedValue` wasn't working. People don't read the documentation. I recommend that you read the documentation, rather than posting wrong information on StackOverflow. – 15ee8f99-57ff-4f92-890c-b56153 Jun 16 '16 at 12:47
  • [Here's the documentation](https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedvaluepath(v=vs.110).aspx). HTH. – 15ee8f99-57ff-4f92-890c-b56153 Jun 16 '16 at 12:58
  • This worked for me, i was missing: UpdateSourceTrigger=PropertyChanged – Jon Barker Dec 22 '17 at 17:27