Binding a Listbox's display and actual values to an XML node
I hav开发者_Go百科e a set of XML as follows:
...<spotTerms>
<terms xmlns= "">
<term tag="1m" display="1M"/>
<term tag="3m" display="3M"/>
<term tag="6m" display="6M"/>...
I have successfully bound a listbox to this XML using the following:
XmlDataProvider x:Key="Symbols" XPath="/symbols" Source="Config\Symbols.xml"/>
...
<ListBox x:Name="SpotMonths"
Style="{StaticResource SymbolChooserListBox}"
ItemsSource="{Binding Source={StaticResource Symbols}, XPath=spotTerms/terms/term/@display}"
SelectionMode="Multiple"
HorizontalContentAlignment="Stretch" Background="#00000000"/>
My plan is to build strings depending on what items have been selected. To this end, I would like to be able to use one XML attribute for display and another for the actual string concatenation.
e.g. I would like to be able to display "1M" but use "1m" at the back end.
Can anyone help with the syntax for this?
EDIT: Robertos's answer helped me out. In case others have the same problem, this is the final piece of XAML I used:
<ListBox x:Name="SpotMonths"
Style="{StaticResource SymbolChooserListBox}"
ItemsSource="{Binding Source={StaticResource Terms}}"
SelectedValuePath="@tag"
DisplayMemberPath="@display"
SelectionMode="Multiple"
HorizontalContentAlignment="Stretch" Background="#00000000" />
The property you're looking for is ItemsControl.DisplayMemberPath.
Your code might look like this:
<XmlDataProvider x:Key="Terms" XPath="/symbols/spotTerms/terms/term" Source="Config\Symbols.xml"/>
<ListBox x:Name="SpotMonths"
Style="{StaticResource SymbolChooserListBox}"
ItemsSource="{Binding Source={StaticResource Terms}, XPath=@tag}"
SelectionMode="Multiple"
HorizontalContentAlignment="Stretch" Background="#00000000"
DisplayMemberPath="@display" />
精彩评论