2009-12-15 79 views
1

我有以下XML(簡化示例),它應該在兩個ComboBox之間具有父/子關係。父母綁定正確,但孩子不綁定父母SelectedItem如何綁定父/子ComboBox對XML數據源?

當我將xmlns設置爲foobar XML並刪除所有名稱空間引用時,它按預期工作。另外,如果我將ItemsSource="{Binding XPath=fb:foo/fb:bars/fb:bar}"設置爲comboBar,則它會從foo元素中找到所有bar節點。

實例(測試中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 [email protected]}" /> 
    </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> 

回答

1

如果你要使用Visual Studio的替代XamlPad,你會看到你得到一個XPathException的聲明命名空間管理器或XsltContext需要。

<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 [email protected]}" /> 
    </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>