views:

203

answers:

1

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.

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.

EXAMPLE (tested to work in XamlPad)

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;

  <Page.Resources>
    <XmlDataProvider x:Key="foobarSource" XPath="fb:foobar">
      <XmlDataProvider.XmlNamespaceManager>
        <XmlNamespaceMappingCollection>
          <XmlNamespaceMapping
            Prefix="fb" Uri="http://foo.bar/1.0/foobar.xsd"/&gt;
        </XmlNamespaceMappingCollection>
      </XmlDataProvider.XmlNamespaceManager>
      <x:XData>
        <foobar xmlns="http://foo.bar/1.0/foobar.xsd"&gt;
          <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>
+1  A: 

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"&gt;
                <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>

Wallstreet Programmer