views:

34

answers:

2

Hi,

I've got a ComboBox with an ItemsSource which I've bound to a List(Of String).

What I'd like to do is have the XAML update a String property when the SelectedValue of the ComboBox changes. I've seen a whole bunch of examples for TextBoxes which use

Text="{Binding Path=MyString}"

sort of stuff, but I don't really think that'll be the way to go if, in future, I need to change the ItemsSource to a List(Of ObscureObject)...

+1  A: 

Binding to the selected property of a combobox is fairly simple.

XAML :

<ComboBox ItemsSource={Binding Path=MyCollection} SelectedItem={Binding Path=MyItem}/>

CodeBehind :

public List<string> MyCollection {get; set;}
public string MyItem {get; set;}

If you want to insert text into the selected item, you'll need to use INotifyPropertyChanged

as for your scalability issue, its a fairly minor change to update the type of a property to reflect a collection. Otherwise you could try binding to an Object although that would mean you would constantly have to recast the object back to the state you want.

Val
A: 

You can use SelectedItem property of ComboBox to achieve this.

    <ComboBox ItemsSource="{Binding Path=YouList}" 
SelectedItem="{Binding Path=MyString}" />

When you change your list in future you will have to bind the SelectedItem with a property of your objects type.

Have a look at this article for more details -

http://japikse.blogspot.com/2008/10/wpf-combobox-selecteditem-selectedvalue.html

akjoshi