绑定到XML节点名称
问题描述:
我想绑定到我的WPF应用程序的一些XML数据。我已经设置了数据上下文,这样我试图绑定到的XmlElement最终看起来像这样:绑定到XML节点名称
<Item name="Potion" classes="Healing Item" value="200">
<Classes>
<Class value="HealingItem" />
</Classes>
<Description value="A low quality potion, it restores a small amount of health" />
<Components>
<HealingComponent>
<BattleHealingComponent>
<HPHealingComponent value="500" type="Absolute"/>
</BattleHealingComponent>
</HealingComponent>
</Components>
</Item>
现在,这里的问题。我无法弄清楚我可以绑定的XPath查询将只返回组件子节点。
我知道它会去是这样的:
ItemsSource="{Binding XPath=Components/*/????}"
我卡在使用什么????
此查询结果应显示“HealingComponent”我试过在线XPath可视化器上使用各种不同的参数,但我无法弄清楚这一点。我准备好了名字(),但我似乎无法让它工作。
任何帮助,将不胜感激
答
除了ItemsSource
你可能需要一个ItemTemplate
,这应该工作:
<ListBox ItemsSource="{Binding XPath=Components/*}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
如果你不打算做任何幻想,你也可以使用DisplayMemberPath
,尽管在这种情况下绑定确保Name
是而不是解释为XPath,但您可能没有使用DisplayMemberPath
进行那种控制。
工作完美。谢谢 – Megatron