XmlDataProvider绑定控件与元素子项

时间:2014-07-03 19:13:02

标签: xml wpf xpath binding xmldataprovider

在以下代码中,我无法访问XML文件中的参数元素。 ListBox显示XML文件中的所有指令。 ComboBox应该显示与ListBox中所选指令相关的所有Parameters元素。 ComboBox的内容是我遇到问题的地方。下面提供的代码没有显示任何内容。

<Window x:Class="LinqToXmlDataBinding.L2XDBForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF Data Binding using LINQ-to-XML" Height="750" Width="500" ResizeMode="CanResize">

<Window.Resources>
    <XmlDataProvider x:Key="XMLInstructionsMapping" Source="XMLMapping.xml"       XPath="InstructionsMapping/Instruction"/>

    <!-- Template for use in Books List listbox. -->
    <DataTemplate x:Key="InstructionTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Margin="3" Text="{Binding XPath=@Name}"/>
            <TextBlock Margin="3" Text="-"/>
            <TextBlock Margin="3" Text="ConvertedFrom: "/>
            <TextBlock Margin="3" Text="{Binding XPath=@ConvertedFrom}"/>
        </StackPanel>          
    </DataTemplate>
    <DataTemplate x:Key="ParamterTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Margin="3" Text="Name: "/>
            <TextBlock Margin="3" Text="{Binding XPath=@Name}"/>
            <TextBlock Margin="3" Text="-"/>
            <TextBlock Margin="3" Text="DataType: "/>
            <TextBlock Margin="3" Text="{Binding XPath=@DataType}"/>
            <TextBlock Margin="3" Text="-"/>
            <TextBlock Margin="3" Text="Direction: "/>
            <TextBlock Margin="3" Text="{Binding XPath=@Direction}"/>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

<!-- Main visual content container -->
<StackPanel Background="lightblue" DataContext="{Binding Source={StaticResource XMLInstructionsMapping}}">

    <!-- List box to display all instructions section -->
    <DockPanel Margin="5">
        <Label  Background="Gray" FontSize="12" BorderBrush="Black" BorderThickness="1" FontWeight="Bold">Instruction List
            <Label.LayoutTransform>
                <RotateTransform Angle="90"/>
            </Label.LayoutTransform>
        </Label>

        <ListBox x:Name="lbBooks" Height="200" Width="415" 
                 ItemsSource="{Binding Source={StaticResource XMLInstructionsMapping}}"
                 ItemTemplate ="{StaticResource InstructionTemplate}"                    
                 IsSynchronizedWithCurrentItem="True" SelectionMode="Single" Visibility="Visible">
        </ListBox>            
    </DockPanel>

    <Label  Background="Gray" FontSize="12" BorderBrush="Black" BorderThickness="1" FontWeight="Bold">Parameter List
    </Label>
    <!-- Combobox to display all selected instruction's parameters -->
    <ComboBox x:Name="lstParams" Margin="5" Height="30" Width="415"
                 ItemsSource="{Binding Source={StaticResource XMLInstructionsMapping}, XPath=InstructionsMapping/Instruction/Parameters/Parameter}"
                 ItemTemplate ="{StaticResource ParamterTemplate}"                    
                 IsSynchronizedWithCurrentItem="True" Visibility="Visible">
    </ComboBox>  
</StackPanel>

这是我要绑定的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<InstructionsMapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Instruction Name="XIE" ConvertedFrom="XIC" >
    <Parameters>
      <Parameter Name="In1" Direction="Input" DataType="Bool" />
      <Parameter Name="In1" Direction="Input" DataType="Bool" />
      <Parameter Name="In1" Direction="Input" DataType="Bool" />
      <Parameter Name="In1" Direction="Input" DataType="Bool" />
      <Parameter Name="In1" Direction="Input" DataType="Bool" />
    </Parameters>
  </Instruction>
  <Instruction Name="XIC" ConvertedFrom="XIC" >
    <Parameters>
      <Parameter Name="In1" Direction="Input" DataType="Bool" />
      <Parameter Name="In1" Direction="Input" DataType="Bool" />
      <Parameter Name="In1" Direction="Input" DataType="Bool" />
      <Parameter Name="In1" Direction="Input" DataType="Bool" />
      <Parameter Name="In1" Direction="Input" DataType="Bool" />
    </Parameters>
  </Instruction>
</InstructionsMapping>

我正在尝试使用ListBox

中所选指令的参数填充ComboBox

我正在使用XmlDataProvider与XML文件绑定。我无法弄清楚我需要的XPath表达式(可能是我缺少其他东西)以便显示指令元素的子元素。

任何帮助都会受到必要的XPath表达式的欢迎。

1 个答案:

答案 0 :(得分:0)

首先,从资源 XPath 中删除 XMLInstructionsMapping 。声明如下:

<XmlDataProvider x:Key="XMLInstructionsMapping" Source="XMLMapping.xml"/>

Explanation - 让XMLDataProvider加载完整的XML文件,而不是XML中的特定节点。


第二次,在 XPath 的ItemsSource上设置 ListBox ,如下所示:

<ListBox x:Name="lbBooks" Height="200" Width="415" 
         ItemsSource="{Binding Source={StaticResource XMLInstructionsMapping},
                               XPath=InstructionsMapping/Instruction}" <-- HERE
         ItemTemplate ="{StaticResource InstructionTemplate}"                    
         IsSynchronizedWithCurrentItem="True" SelectionMode="Single" 
         Visibility="Visible">
</ListBox>

Explanation - 将XPath从资源移至此处以获取特定节点。


第三次,将 XPath ItemsSource中的 comboBox 更新为:

<ComboBox x:Name="lstParams" Margin="5" Height="30" Width="415"
        ItemsSource="{Binding Source={StaticResource XMLInstructionsMapping}, 
                       XPath=InstructionsMapping/Parameters/Parameter}" <-- HERE
        ItemTemplate ="{StaticResource ParamterTemplate}"                    
        IsSynchronizedWithCurrentItem="True" Visibility="Visible">
</ComboBox>

Explanation - 将正确的XPath设置为指向您希望comboBox填充的节点。


<强>更新

如果您只想显示与listBox中所选项目相对应的项目,您可以使用 ElementName 与ChildNodes绑定,如下所示:

<ComboBox x:Name="lstParams" Margin="5" Height="30" Width="415"
        ItemsSource="{Binding Path=SelectedItem.ChildNodes[0].ChildNodes, 
                              ElementName=lbBooks}"
        ItemTemplate ="{StaticResource ParamterTemplate}"                    
        IsSynchronizedWithCurrentItem="True" Visibility="Visible">
</ComboBox>