【问题标题】:How do I bind to different property on ComboBox SelectedItem?如何绑定到 ComboBox SelectedItem 上的不同属性?
【发布时间】:2010-07-26 17:38:04
【问题描述】:

我在 WPF 中有一个这样的组合框:

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

项目在哪里:

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

Test 是 ViewModel 上对象类型的属性,设置为窗口的数据上下文。

一切正常,我的 Test 属性接收 XmlNode 对象,这是有道理的。

但是,我想从该 xml 接收不同的属性,例如 XPath=item/@value

我该怎么做?

【问题讨论】:

    标签: wpf data-binding combobox selecteditem


    【解决方案1】:

    使用DisplayMemberPathSelectedValuePath

    <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}"/>
    

    选中的项目将是item 元素,它将显示name 属性,并将id 属性绑定到Test

    【讨论】:

    • 谢谢!这是非常甜蜜的。我不知道那些属性支持 XPath。
    猜你喜欢
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 2011-05-02
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 2010-10-24
    相关资源
    最近更新 更多