开发者

How do I bind to different property on ComboBox SelectedItem?

开发者 https://www.devze.com 2023-01-08 18:03 出处:网络
I have a combobox in WPF like this: <ComboBox Text=\"Select 开发者_如何学编程Language...\" IsEditable=\"True\" IsReadOnly=\"True\"

I have a combobox in WPF like this:

<ComboBox Text="Select 开发者_如何学编程Language..." IsEditable="True" IsReadOnly="True"
          ItemsSource="{Binding XPath=item/@name, Source={StaticResource Items}}"
          SelectedItem="{Binding Path=Test, Mode=OneWayToSource}"/>

Where Items is:

<XmlDataProvider x:Key="Items" Source="/itemlist.xml" XPath="/itemlist"/>

Test is a property of type object on ViewModel that is set as datacontext of a window.

Everything works fine, and my Test property receives XmlNode object, which makes sense.

However, I would like to receive different attribute from that xml for example XPath=item/@value

How do I do that?


Use DisplayMemberPath and SelectedValuePath:

<ComboBox Text="Select Language..." IsEditable="True" IsReadOnly="True"
  ItemsSource="{Binding XPath=item, Source={StaticResource Items}}"
  DisplayMemberPath="@name"
  SelectedValuePath="@id"
  SelectedValue="{Binding Path=Test, Mode=OneWayToSource}"/>

The selected item will be the item element, it will display the name attribute, and it will bind the id attribute to Test.

0

精彩评论

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