2010-08-26 57 views
1

我想创建一个将绑定到XPath的列表框,相对于其他列表框当前选定的项目。XAML /混合从属数据绑定

它使用XmlDataProvider的数据和XML文件看起来像这样:

<Programs> 
    <Program name="..."> 
     <Step name="..."/> 
     <Step name="..."/> 
    </Program> 
    <Program name="another"> 

    ... 

</Programs 

所以,“父”列表框列出了所有的程序,而“子”表示从当前仅几步之遥程序。 什么是这种类型的绑定调用?

回答

2

在这里,你去。希望这回答你的问题。

<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 [email protected]}"/> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

      <ListBox DataContext="{Binding ElementName=parent, Path=SelectedItem}" ItemsSource="{Binding XPath=Step}" 
        Height="100"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding [email protected]}"/> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     </StackPanel> 

    </Grid> 
</Window> 
+0

对不起,我花了一段时间才回答,我不在电脑附近一段时间。谢谢,这清除了一些事情! – Johnny 2010-08-28 13:55:32