问题
I want to create a listbox that'll be bound to XPath, relative to the other listbox's currently selected item.
It's using XmlDataProvider for the data, and the XML file looks like this:
<Programs>
<Program name="...">
<Step name="..."/>
<Step name="..."/>
</Program>
<Program name="another">
...
</Programs
So, the "parent" listbox is listing out all the programs, while "child" shows only steps from the current program. What's such a type of binding called?
回答1:
Here you go. Hope this answers your question.
<Window x:Class="StackOverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:StackOverflow"
xmlns:uc="clr-namespace:StackOverflow.UserControls"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="xml">
<x:XData>
<Programs xmlns="">
<Program name="Program">
<Step name="Step1"/>
<Step name="Step2"/>
</Program>
<Program name="Program2">
<Step name="Step3"/>
<Step name="Step4"/>
</Program>
</Programs>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid>
<StackPanel>
<ListBox x:Name="parent" ItemsSource="{Binding Source={StaticResource xml}, XPath=Programs/Program}"
Height="100">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=@name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox DataContext="{Binding ElementName=parent, Path=SelectedItem}" ItemsSource="{Binding XPath=Step}"
Height="100">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=@name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
</Window>
来源:https://stackoverflow.com/questions/3579866/xaml-blend-dependent-data-binding