I have XML in the following format:
<Products>
<Product name="MyProduct1">
<Components>
<Component name="MyComponent1">
<Teams>
<Team id="1"/>
<Team id="4"/>
</Teams>
</Component>
</Components>
</Product>
</Products>
This is stored in an external XML file that is included via an XmlDataProvider
in my XAML:
<XmlDataProvider Source="Products.xml"
XPath="Products/Product"
x:Key="productsXml"/>
You'll see that the Team
nodes have only an id
attribute; this links them to other XML that is embedded directly in my XAML:
<XmlDataProvider x:Key="teamsXml" XPath="Teams/Team">
<x:XData>
<Teams xmlns="">
<Team name="Team A" id="1"/>
<Team name="Team B" id="2"/>
<Team name="Team C" id="3"/>
<Team name="Team D" id="4"/>
</Teams>
</x:XData>
</XmlDataProvider>
I have the following ComboBox in my XAML:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="133,217,0,0"
Name="cbo_team" VerticalAlignment="Top" Width="148"
DataContext="{Binding ElementName=cbo_component, Path=SelectedItem}"
ItemsSource="{Binding XPath=Teams/Team}"
SelectedIndex="0" />
This ComboBox shows a list of Teams associated with the currently selected Component. I need to keep DataContext
and ItemsSource
the same here so that only the related Teams are shown, and not all available Teams. However, I don't want to show the Team id
attribute in the ComboBox, I want to show the name
attribute as it is defined in teamsXml
, e.g., Team A. How can I use a DataTemplate
and somehow get the Team name from teamsXml
when I have the Team id
from productsXml
? I also want the value of the ComboBox to be the name
attribute.
Edit:
I'm wondering if something like this is a step in the right direction:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="133,217,0,0"
Name="cbo_team" VerticalAlignment="Top" Width="148"
DataContext="{Binding ElementName=cbo_component, Path=SelectedItem}"
ItemsSource="{Binding XPath=Teams/Team}"
SelectedIndex="0">
<ComboBox.DisplayMemberPath>
<Binding Source="{StaticResource teamsXml}"
XPath="???"/> <!-- Need to select the @name attribute of the node
whose @id attribute is the same value as the
current selected item in this ComboBox -->
</ComboBox.DisplayMemberPath>
</ComboBox>