在以下代码中,我无法访问 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 表达式的帮助都会受到欢迎。