Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Combobox binding with List<string>

Tags:

c#

wpf

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.

like image 836
mo alaz Avatar asked Feb 20 '14 04:02

mo alaz


1 Answers

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

like image 78
Chris Avatar answered Sep 28 '22 02:09

Chris