我有类似以下的XML
<?xml version="1.0" encoding="utf-8"?>
<foo name="FooBar" xmlns="http://mydomain/myapp/ver/myschema.xsd">
<bars v="test">
<bar bat="one"/>
<bar bat="two"/>
<bar bat="three"/>
</bars>
</foo>
如何在wpf中映射它,如果不设置默认名称空间,它就会工作,但是当我
xmlns
设置我的绑定与任何内容都不匹配。我尝试过以下声明,但是我很难找到如何映射组合框,如下所示。
<Window.Resources>
<!-- works if xmlns is not set -->
<XmlDataProvider x:Key="mySource1"
XPath="/foo">
<!-- also tried -->
<XmlDataProvider x:Key="mySource2"
XPath="//*[local-name()='foo']">
<!-- also tried -->
<XmlDataProvider x:Key="mySource3"
XPath="/foo">
<XmlDataProvider.XmlNamespaceManager>
<XmlNamespaceMappingCollection>
<XmlNamespaceMapping
Uri="http://mydomain/myapp/ver/myschema.xsd"
Prefix=""/>
</XmlNamespaceMappingCollection>
</XmlDataProvider.XmlNamespaceManager>
</XmlDataProvider>
</Window.Resources>
<StackPanel DataContext="{StaticResource mySource1}">
<Label Content="{Binding XPath=@name}"/>
<Label DataContext="{Binding XPath=bars}"
Content="{Binding XPath=@v}"/>
</StackPanel>
我正在分配
XmlDataProvider.Source
属性。
XmlDataProvider xdp = FindResource("mySource1") as XmlDataProvider;
// ... setup dialog and confirm resource.
using (Stream s = dlg.OpenFile()) {
XmlDocument doc = new XmlDocument();
doc.Load(s);
xdp.Document = doc;
}
任何帮助解决这一点都将非常感谢,谢谢。
更新
根据安德鲁斯的建议,我提出了以下建议:
<Window.Resources>
<XmlDataProvider x:Key="mySource"
XPath="/fb:foo">
<XmlDataProvider.XmlNamespaceManager>
<XmlNamespaceMappingCollection>
<XmlNamespaceMapping
Uri="http://mydomain/myapp/ver/myschema.xsd"
Prefix="fb"/>
</XmlNamespaceMappingCollection>
</XmlDataProvider.XmlNamespaceManager>
</XmlDataProvider>
</Window.Resources>
<StackPanel DataContext="{StaticResource mySource}">
<Label Content="{Binding XPath=@name}"/>
<ComboBox ItemsSource="{Binding XPath=fb:bars/fb:bar}"
IsSynchronizedWithCurrentItem="True">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=@bat}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>