【问题标题】:Bind ObservableCollection<XmlNode> to combobox wpf将 ObservableCollection<XmlNode> 绑定到组合框 wpf
【发布时间】:2012-06-27 10:04:55
【问题描述】:

我正在尝试将组合框绑定到 ObservableCollection。当显示表单时,组合框为空。与字符串类型的 ObservableCollection 相同的代码可以完美运行。我有一种感觉,我的 XPath 是错误的。欢迎提出任何建议:

XAML:

<ComboBox ItemsSource="{Binding ItemParameters, XPath=InnerXml/name,Mode=TwoWay}" SelectedIndex="0" Margin="2" VerticalAlignment="Top" HorizontalContentAlignment="Stretch" Grid.Row="1" Grid.Column="1" Height="24"  />

ObservableCollection XmlNode:

public ObservableCollection<XmlNode> _itemParameters = new ObservableCollection<XmlNode>();
public ObservableCollection<XmlNode> ItemParameters
{
    get { return _itemParameters; }
    set { _itemParameters = value; }
}

组合框应该显示集合中每个 XmlNode 的 name 属性:

更新:

我已尝试以两种不同的方式使用 DisplayMemberPath,但组合框仍然不包含数据:

DisplayMemberPath="{Binding XPath=name}" ItemsSource="{Binding ItemParameters}"
DisplayMemberPath="{Binding XPath=InnerXml/name}" ItemsSource="{Binding ItemParameters}"

解决方案:

这成功了,希望对其他人也有帮助:

<ComboBox DisplayMemberPath="@name" ItemsSource="{Binding ItemParameters}"

【问题讨论】:

标签: c# wpf .net-4.0


【解决方案1】:

首先你同时设置了PathXPath,这是相互冲突的属性,其次你绑定了ItemsSource,这与你要显示的内容无关里面 项目。为此,请使用 DisplayMemberPathItemTemplateItemsSource 应该只绑定到 ItemParameters

【讨论】:

  • 感谢您的回答,我相信我不会同时设置 Path 和 Xpath...如果您查看 XAML,您会发现只设置了 XPath。还是我错过了什么?
  • @Deni: ItemParameters 设置为 Path 属性,它是隐式的
  • @Deni:一般在markup extensions中,未命名的参数被传递给底层类的构造函数,Binding有一个constructor which takes a string作为Binding.Path使用。
猜你喜欢
  • 1970-01-01
  • 2020-10-06
  • 2018-09-03
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多