0

在以下代码中,我无法访问 XML 文件中的参数元素。ListBox 显示 XML 文件中的所有指令。ComboBox 应该显示与 ListBox 中所选指令相关的所有参数元素。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

我正在使用一个XmlDataProvider来绑定 XML 文件。我无法弄清楚我需要的 XPath 表达式(也许我遗漏了其他东西)才能显示指令元素的子元素。

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

4

1 回答 1

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,在comboBoxItemsSource 中更新为:

<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 以指向您希望组合框填充的节点。


更新

如果您只想显示与在列表框中选择的项目相对应的项目,您可以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>
于 2014-07-03T19:25:12.767 回答