开发者

How do I bind the SelectedValue of a ComboBox to a Property?

I've got a ComboBox with an ItemsSource 开发者_StackOverflow中文版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)...


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜