I'm using XmlDataProvider to bind some UI controls with XML file content.
In the following code, the DataGrid displays the list of instructions in the XML file.
I have two ComboBox
. The first ComboBox
should contain the selected instruction's (in the DataGrid) childNodes having the Direction attribute equal to Input.
The second ComboBox
should should contain the selected instruction's (in the DataGrid) childNodes having the Direction attribute equal to Output.
My problem is that I'm unable to find out the XPath expression necessary to fill the ItemsSource of the ComboBox
controls in order to get what I want.
Actually it displays all the ChildNodes in both comboBox.
Here is the XAML markup:
<Window x:Class="XmlProviderDataGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<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>
<Grid ShowGridLines="True">
<Grid.DataContext>
<XmlDataProvider Source="XMLMapping.xml" XPath="InstructionsMapping/Instruction"/>
</Grid.DataContext>
<Grid.RowDefinitions>
<RowDefinition Height="150*" />
<RowDefinition Height="181*" />
<RowDefinition Height="181*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250*"/>
<ColumnDefinition Width="260*" />
</Grid.ColumnDefinitions>
<DataGrid x:Name="dataGrid1" Margin="8,8,8,0" Grid.Row="0" Grid.ColumnSpan="2"
AutoGenerateColumns="False" ItemsSource="{Binding}"
Height="100" VerticalAlignment="Top"
IsSynchronizedWithCurrentItem="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding XPath=@Name}" Header="Name"/>
<DataGridTextColumn Binding="{Binding XPath=@ConvertedFrom}" Header="ConvertedFrom"/>
<DataGridTextColumn Binding="{Binding XPath=@InstType}" Header="Type"/>
<DataGridTextColumn Binding="{Binding XPath=@Data}" Header="Data file"/>
<DataGridTextColumn Binding="{Binding XPath=@Inputs}" Header="Inputs"/>
</DataGrid.Columns>
</DataGrid>
<Button Margin="5" VerticalAlignment="Top" Grid.Row="1" Grid.Column="0" Content="New Instruction" Click="OnCreateNewInstruction"/>
<Button Margin="5" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" Content="Edit Instruction" Click="OnEditNewInstruction"/>
<StackPanel Grid.Row="2" Grid.Column="1">
<Label Background="Gray" FontSize="12" BorderBrush="Black" BorderThickness="1" FontWeight="Bold" >Output Parameters:</Label>
<ComboBox x:Name="lstOutput" Margin="5" VerticalAlignment="Top" HorizontalAlignment="Stretch"
ItemsSource="{Binding Path=SelectedItem, ElementName=dataGrid1, UpdateSourceTrigger=PropertyChanged}"
ItemTemplate ="{StaticResource ParamterTemplate}"
IsSynchronizedWithCurrentItem="True" Visibility="Visible">
</ComboBox>
</StackPanel>
<StackPanel Grid.Row="2" Grid.Column="0">
<Label Background="Gray" FontSize="12" BorderBrush="Black" BorderThickness="1" FontWeight="Bold" Grid.Column="0">Input Parameters:</Label>
<ComboBox x:Name="lstInput" Margin="5,5,5,5" VerticalAlignment="Top" HorizontalAlignment="Stretch"
ItemsSource="{Binding Path=SelectedItem, ElementName=dataGrid1, UpdateSourceTrigger=PropertyChanged}"
ItemTemplate ="{StaticResource ParamterTemplate}"
IsSynchronizedWithCurrentItem="True" Visibility="Visible">
</ComboBox>
</StackPanel>
</Grid>
</Window>
Here you can find my used xml file:
<?xml version="1.0" encoding="utf-8"?>
<InstructionsMapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" CatalogID="Micro_8">
<Instruction Name="XIC" ConvertedFrom="XIC" DataFilePath="\Project18_v1.7z">
<Parameter Name="Out14" Direction="Output" DataType="Bool" />
<Parameter Name="Out2" Direction="Output" DataType="Bool" />
<Parameter Name="In3" Direction="Input" DataType="Real" />
<Parameter Name="In11" Direction="Input" DataType="Real" />
<Parameter Name="In13" Direction="Input" DataType="Dint" />
</Instruction>
<Instruction Name="OTE" ConvertedFrom="OTE" DataFilePath="\Project18_v1.7z">
<Parameter Name="In1" Direction="Input" DataType="Bool" />
<Parameter Name="In2" Direction="Input" DataType="Dint" />
<Parameter Name="In3" Direction="Input" DataType="Real" />
<Parameter Name="Ou1" Direction="Output" DataType="Bool" />
<Parameter Name="Out2" Direction="Output" DataType="Bool" />
</Instruction>
<Instruction Name="TON" ConvertedFrom="TON" DataFilePath="\Project18_v1.7z">
<Parameter Name="In1" Direction="Input" DataType="Bool" />
<Parameter Name="In2" Direction="Input" DataType="Dint" />
<Parameter Name="In3" Direction="Input" DataType="Real" />
<Parameter Name="Ou1" Direction="Output" DataType="Bool" />
<Parameter Name="Out2" Direction="Output" DataType="Bool" />
</Instruction>
<Instruction Name="ADD" ConvertedFrom="+" DataFilePath="\Project18_v1.7z">
<Parameter Name="In1" Direction="Input" DataType="Bool" />
<Parameter Name="In2" Direction="Input" DataType="Real" />
<Parameter Name="In3" Direction="Input" DataType="Real" />
<Parameter Name="In4" Direction="Input" DataType="Real" />
<Parameter Name="In5" Direction="Input" DataType="Bool" />
</Instruction>
<Instruction Name="ADD" ConvertedFrom="+" DataFilePath="\Project18_v1.7z">
<Parameter Name="In1" Direction="Output" DataType="Bool" />
<Parameter Name="In2" Direction="Output" DataType="Bool" />
<Parameter Name="In3" Direction="Output" DataType="Dint" />
<Parameter Name="In4" Direction="Output" DataType="Bool" />
<Parameter Name="In5" Direction="Output" DataType="Dint" />
</Instruction>
</InstructionsMapping>