WPF绑定到一个元素属性的XPath可达的价值
我想绑定到一个值可达只使用XPath从元素的属性。
I'd like to bind to a value reachable only with XPath from the property of an element.
元素填充一些XML的一个组合框,它的属性是的SelectedItem。将selectedItem指向一个XML元素,我想结合在一个子元素的值,可以使用XPath来达到。
The element is a ComboBox populated from some XML, and its property is SelectedItem. SelectedItem points to an XML element, and I'd like to bind to a child element's value within that, which can be reached with an XPath.
在XAML看起来像这样,到目前为止:
The XAML looks like this, so far:
<StackPanel Orientation="Vertical" Margin="10,10">
<StackPanel Orientation="Horizontal">
<Label>Partner</Label>
<ComboBox Name="Partner" Margin="10,0"
ItemsSource="{Binding XPath=/Root/Tables/Partners/row}"
ItemTemplate="{StaticResource Partner}"/>
</StackPanel>
<Button Margin="25,15" Name="Submit" Width="100" Height="30" IsDefault="True"
CommandParameter="{Binding ElementName=Partner, Path=SelectedItem}">
Okay
</Button>
</StackPanel>
源XML看起来是这样的:
The source XML looks like this:
<Root>
<Tables>
<Partners>
<row>
<PartnerID>1</PartnerID>
<Name>FooBar.Com</Name>
</row>
<row>
.
.
.
</row>
</Partners>
</Tables>
</Root>
我的问题是Button的CommandParameter与在它太多的信息结合到一个的XmlElement。我想有CommandParameter指一个子元素,有点像如果我能指定用的XPath = PartnerID额外的向下钻取到返回我真正感兴趣的整数值。
My problem is that the Button's CommandParameter is binding to an XmlElement with too much information in it. I'd like to have CommandParameter refer to a child element, kind of like if I could specify an extra drill-down with "XPath=PartnerID" to return the integer value that I'm really interested in.
最后计算出来自己。解决的办法是设置按钮,组合框的的SelectedItem的DataContext的,然后设置CommandParameter到XPath的结合,是这样的:
Ended up figuring it out myself. The solution was to set the DataContext of the button to the combobox's SelectedItem, then set the CommandParameter to an XPath binding, like this:
<Button DataContext="{Binding ElementName=Partner, Path=SelectedItem}"
Margin="25,15" Name="Submit" Width="100" Height="30" IsDefault="True"
CommandParameter="{Binding XPath=PartnerID/text()}">Okay</Button>