如何绑定父/子组合框与XML数据源? [英] How can I bind parent/child ComboBox's against XML datasource?
问题描述
我有以下XML(简化示例),它应该有两个 ComboBox
之间的父/子关系。父母正确绑定,但孩子不绑定到父母 SelectedItem
。
I have the following XML (simplified example) which should have a parent/child relationship between two ComboBox
's. The parent binds correctly but the child does not bind to the parents SelectedItem
.
当我设置 xmlns
针对 foobar
XML,并删除所有名称空间引用它按预期工作。另外,如果我设置 ItemsSource ={Binding XPath = fb:foo / fb:bars / fb:bar}
对 comboBar
它从 foo
元素中查找所有 bar
节点。
When I set xmlns
against the foobar
XML and remove all namespace references it works as expected. Also if I set ItemsSource="{Binding XPath=fb:foo/fb:bars/fb:bar}"
against comboBar
it finds all the bar
nodes as expected from both foo
elements.
示例(在XamlPad中测试工作)
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<XmlDataProvider x:Key="foobarSource" XPath="fb:foobar">
<XmlDataProvider.XmlNamespaceManager>
<XmlNamespaceMappingCollection>
<XmlNamespaceMapping
Prefix="fb" Uri="http://foo.bar/1.0/foobar.xsd"/>
</XmlNamespaceMappingCollection>
</XmlDataProvider.XmlNamespaceManager>
<x:XData>
<foobar xmlns="http://foo.bar/1.0/foobar.xsd">
<foo name="Foo 1">
<bars name='bars 1'>
<bar name="first"/>
<bar name="second"/>
</bars>
</foo>
<foo name="Foo 2">
<bars name='bars 2'>
<bar name="third"/>
<bar name="fourth"/>
</bars>
</foo>
</foobar>
</x:XData>
</XmlDataProvider>
<DataTemplate x:Key="comboTemplate">
<TextBlock Text="{Binding XPath=@name}" />
</DataTemplate>
</Page.Resources>
<StackPanel DataContext="{StaticResource foobarSource}">
<ComboBox Width="150" x:Name="comboFoo"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding XPath=fb:foo}"
ItemTemplate="{StaticResource comboTemplate}"/>
<ComboBox Width="150" x:Name="comboBar"
IsSynchronizedWithCurrentItem="True"
DataContext="{Binding SelectedItem, ElementName=comboFoo}"
ItemsSource="{Binding XPath=fb:bars/fb:bar}"
ItemTemplate="{StaticResource comboTemplate}"/>
</StackPanel>
</Page>
推荐答案
如果您使用Visual Studio而不是XamlPad,会看到您收到一个需要命名空间管理器或XsltContext的XPathException。
If you would have used Visual Studio instead of XamlPad you would see that you get an XPathException stating Namespace Manager or XsltContext needed.
<Window.Resources>
<XmlNamespaceMappingCollection x:Key="fbNamespaces">
<XmlNamespaceMapping Prefix="fb" Uri="http://foo.bar/1.0/foobar.xsd" />
</XmlNamespaceMappingCollection>
<XmlDataProvider x:Key="foobarSource" XPath="fb:foobar">
<XmlDataProvider.XmlNamespaceManager>
<XmlNamespaceMappingCollection>
<XmlNamespaceMapping Prefix="fb" Uri="http://foo.bar/1.0/foobar.xsd" />
</XmlNamespaceMappingCollection>
</XmlDataProvider.XmlNamespaceManager>
<x:XData>
<foobar xmlns="http://foo.bar/1.0/foobar.xsd">
<foo name="Foo 1">
<bars name='bars 1'>
<bar name="first" />
<bar name="second" />
</bars>
</foo>
<foo name="Foo 2">
<bars name='bars 2'>
<bar name="third" />
<bar name="fourth" />
</bars>
</foo>
</foobar>
</x:XData>
</XmlDataProvider>
<DataTemplate x:Key="comboTemplate">
<TextBlock Text="{Binding XPath=@name}" />
</DataTemplate>
</Window.Resources>
<StackPanel
Binding.XmlNamespaceManager="{StaticResource fbNamespaces}">
<ComboBox Width="150" x:Name="comboFoo"
IsSynchronizedWithCurrentItem="True"
DataContext="{StaticResource foobarSource}"
ItemsSource="{Binding XPath=fb:foo}"
ItemTemplate="{StaticResource comboTemplate}" />
<ComboBox Width="150"
IsSynchronizedWithCurrentItem="True"
DataContext="{Binding SelectedItem, ElementName=comboFoo}"
ItemsSource="{Binding XPath=fb:bars/fb:bar}"
ItemTemplate="{StaticResource comboTemplate}" />
</StackPanel>
这篇关于如何绑定父/子组合框与XML数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!