带子元素的XmlDataProvider绑定控件



在下面的代码中,我无法到达XML文件中的参数元素。ListBox显示XML文件中的所有指令。组合框应该在列表框中显示与所选指令相关的所有参数元素。组合框的内容是我遇到问题的地方。下面提供的代码不会显示任何内容。

<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表达式,请提供帮助。

第一个,删除 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从资源移到这里以获得特定的节点。


3 ,更新 XPath comboBox ItemsSource:

<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>

相关内容

  • 没有找到相关文章

最新更新