2014-07-11 52 views
-1

我正在使用XmlDataProvider將一些UI控件綁定到XML文件內容。 在下面的代碼中,DataGrid顯示XML文件中的指令列表。我有兩個ComboBox。第一個ComboBox應該包含所選指令的(在DataGrid中)具有方向屬性等於輸入的childNodes。XmlDataProvider過濾所選元素的ChildNodes

第二個ComboBox應該包含方向屬性等於Output的所選指令的(在DataGrid中)childNodes。

我的問題是,我無法找到填充ComboBox控件的ItemsSource所需的XPath表達式,以獲得我想要的。 實際上它顯示了兩個組合框中的所有ChildNode。

這裏是XAML標記:

<Window x:Class="XmlProviderDataGrid.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 

    <Window.Resources> 
     <DataTemplate x:Key="ParamterTemplate"> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Margin="3" Text="Name: "/> 
       <TextBlock Margin="3" Text="{Binding [email protected]}"/> 
       <TextBlock Margin="3" Text="-"/> 
       <TextBlock Margin="3" Text="DataType: "/> 
       <TextBlock Margin="3" Text="{Binding [email protected]}"/> 
       <TextBlock Margin="3" Text="-"/> 
       <TextBlock Margin="3" Text="Direction: "/> 
       <TextBlock Margin="3" Text="{Binding [email protected]}"/> 
      </StackPanel> 
     </DataTemplate> 
    </Window.Resources> 

    <Grid ShowGridLines="True"> 
     <Grid.DataContext> 
      <XmlDataProvider Source="XMLMapping.xml" XPath="InstructionsMapping/Instruction"/> 
     </Grid.DataContext> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="150*" /> 
      <RowDefinition Height="181*" /> 
      <RowDefinition Height="181*" /> 
     </Grid.RowDefinitions> 

     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="250*"/> 
      <ColumnDefinition Width="260*" /> 
     </Grid.ColumnDefinitions> 

     <DataGrid x:Name="dataGrid1" Margin="8,8,8,0" Grid.Row="0" Grid.ColumnSpan="2" 
      AutoGenerateColumns="False" ItemsSource="{Binding}" 
      Height="100" VerticalAlignment="Top" 
      IsSynchronizedWithCurrentItem="True"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Binding="{Binding [email protected]}" Header="Name"/> 
       <DataGridTextColumn Binding="{Binding [email protected]}" Header="ConvertedFrom"/> 
       <DataGridTextColumn Binding="{Binding [email protected]}" Header="Type"/> 
       <DataGridTextColumn Binding="{Binding [email protected]}" Header="Data file"/> 
       <DataGridTextColumn Binding="{Binding [email protected]}" Header="Inputs"/> 
      </DataGrid.Columns> 
     </DataGrid> 

     <Button Margin="5" VerticalAlignment="Top" Grid.Row="1" Grid.Column="0" Content="New Instruction" Click="OnCreateNewInstruction"/> 
     <Button Margin="5" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" Content="Edit Instruction" Click="OnEditNewInstruction"/> 


     <StackPanel Grid.Row="2" Grid.Column="1"> 
      <Label Background="Gray" FontSize="12" BorderBrush="Black" BorderThickness="1" FontWeight="Bold" >Output Parameters:</Label> 
      <ComboBox x:Name="lstOutput" Margin="5" VerticalAlignment="Top" HorizontalAlignment="Stretch" 
         ItemsSource="{Binding Path=SelectedItem, ElementName=dataGrid1, UpdateSourceTrigger=PropertyChanged}" 
         ItemTemplate ="{StaticResource ParamterTemplate}"      
         IsSynchronizedWithCurrentItem="True" Visibility="Visible"> 
      </ComboBox> 
     </StackPanel> 
     <StackPanel Grid.Row="2" Grid.Column="0"> 
     <Label Background="Gray" FontSize="12" BorderBrush="Black" BorderThickness="1" FontWeight="Bold" Grid.Column="0">Input Parameters:</Label> 
      <ComboBox x:Name="lstInput" Margin="5,5,5,5" VerticalAlignment="Top" HorizontalAlignment="Stretch" 
        ItemsSource="{Binding Path=SelectedItem, ElementName=dataGrid1, UpdateSourceTrigger=PropertyChanged}" 
        ItemTemplate ="{StaticResource ParamterTemplate}"      
        IsSynchronizedWithCurrentItem="True" Visibility="Visible"> 
      </ComboBox> 
     </StackPanel> 
     </Grid>  
</Window> 

在這裏,你可以找到我使用的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<InstructionsMapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" CatalogID="Micro_8"> 
    <Instruction Name="XIC" ConvertedFrom="XIC" DataFilePath="\Project18_v1.7z"> 
    <Parameter Name="Out14" Direction="Output" DataType="Bool" /> 
    <Parameter Name="Out2" Direction="Output" DataType="Bool" /> 
    <Parameter Name="In3" Direction="Input" DataType="Real" /> 
    <Parameter Name="In11" Direction="Input" DataType="Real" /> 
    <Parameter Name="In13" Direction="Input" DataType="Dint" /> 
    </Instruction> 
    <Instruction Name="OTE" ConvertedFrom="OTE" DataFilePath="\Project18_v1.7z"> 
    <Parameter Name="In1" Direction="Input" DataType="Bool" /> 
    <Parameter Name="In2" Direction="Input" DataType="Dint" /> 
    <Parameter Name="In3" Direction="Input" DataType="Real" /> 
    <Parameter Name="Ou1" Direction="Output" DataType="Bool" /> 
    <Parameter Name="Out2" Direction="Output" DataType="Bool" /> 
    </Instruction> 
    <Instruction Name="TON" ConvertedFrom="TON" DataFilePath="\Project18_v1.7z"> 
    <Parameter Name="In1" Direction="Input" DataType="Bool" /> 
    <Parameter Name="In2" Direction="Input" DataType="Dint" /> 
    <Parameter Name="In3" Direction="Input" DataType="Real" /> 
    <Parameter Name="Ou1" Direction="Output" DataType="Bool" /> 
    <Parameter Name="Out2" Direction="Output" DataType="Bool" /> 
    </Instruction> 
    <Instruction Name="ADD" ConvertedFrom="+" DataFilePath="\Project18_v1.7z"> 
    <Parameter Name="In1" Direction="Input" DataType="Bool" /> 
    <Parameter Name="In2" Direction="Input" DataType="Real" /> 
    <Parameter Name="In3" Direction="Input" DataType="Real" /> 
    <Parameter Name="In4" Direction="Input" DataType="Real" /> 
    <Parameter Name="In5" Direction="Input" DataType="Bool" /> 
    </Instruction> 
    <Instruction Name="ADD" ConvertedFrom="+" DataFilePath="\Project18_v1.7z"> 
    <Parameter Name="In1" Direction="Output" DataType="Bool" /> 
    <Parameter Name="In2" Direction="Output" DataType="Bool" /> 
    <Parameter Name="In3" Direction="Output" DataType="Dint" /> 
    <Parameter Name="In4" Direction="Output" DataType="Bool" /> 
    <Parameter Name="In5" Direction="Output" DataType="Dint" /> 
    </Instruction> 
</InstructionsMapping> 

回答

0

一種可能的方式:

<ComboBox x:Name="lstInput" Margin="5,5,5,5" VerticalAlignment="Top" HorizontalAlignment="Stretch" 
     DataContext="{Binding Path=SelectedItem, ElementName=dataGrid1, UpdateSourceTrigger=PropertyChanged}" 
     ItemsSource="{Binding XPath=Parameter[@Direction\=\'Input\']}" 
     ItemTemplate ="{StaticResource ParamterTemplate}"      
     IsSynchronizedWithCurrentItem="True" Visibility="Visible"> 
</ComboBox>