Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有元素子级的XmlDataProvider绑定控件_Xml_Wpf_Xpath_Binding_Xmldataprovider - Fatal编程技术网

具有元素子级的XmlDataProvider绑定控件

具有元素子级的XmlDataProvider绑定控件,xml,wpf,xpath,binding,xmldataprovider,Xml,Wpf,Xpath,Binding,Xmldataprovider,在下面的代码中,我无法访问XML文件中的parameters元素。 列表框显示XML文件中的所有说明。 组合框应该在列表框中显示与所选指令相关的所有参数元素。组合框的内容是我遇到问题的地方。下面提供的代码不显示任何内容 <Window x:Class="LinqToXmlDataBinding.L2XDBForm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schema

在下面的代码中,我无法访问XML文件中的parameters元素。 列表框显示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>

我试图用
列表框中所选指令的参数填充组合框

我正在使用
XmlDataProvider
与XML文件绑定。我无法找出显示指令元素的子元素所需的XPath表达式(可能我遗漏了其他内容)


如果需要使用必要的XPath表达式,欢迎提供任何帮助。

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

<XmlDataProvider x:Key="XMLInstructionsMapping" Source="XMLMapping.xml"/>
<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>
<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>
解释
-将XPath从资源移动到此处以获取特定节点


第三个,在
组合框中更新
项目来源:

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

谢谢你的回答,这真的很有帮助…但仍然不完全是我的目标。首先,组合框的XPath应该是:XPath=InstructionsMapping/Instruction/Parameters/Parameters第二,您提供的解决方案使用XML文件中的所有参数填充组合框,而不仅仅是那些属于列表框中所选元素的参数。我怎样才能在组合框中只放入与列表框中所选指令相对应的参数?我猜您是用不同的XML文件编辑问题的,因为我只是从您的问题中复制了XML内容。无论如何,我会用第二个问题更新答案。