我正在搜索几个小时,我正在尝试将XML文件绑定到treeView。 通常,我想在ListBox中选择XML文件,在树视图中打开XML文件。
XML看起来像这样:
<test>
<communication>
<global>
<server id="Server" ip="172.17.10.50" port="5072" />
</global>
<devices>
<device id="Server" ip="172.18.100.50" port="3451" />
<device id="cInterface" ip="172.17.12.52" port="3567" />
<device id="caServer" ip="172.17.12.52" port="3000" />
</devices>
<groups>
<group id="group_cInterface">
<device id="cInterface" />
<device id="Server" />
</group>
</groups>
</communication>
</test>
非常感谢。 如果需要我的全部代码,请随时提问!
修改
可以达到此输出: Output
使用此XAML代码:
<Window x:Class="Communication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="693.55" Width="780">
<Window.Resources>
<HierarchicalDataTemplate x:Key="NodeTemplate"
ItemsSource="{Binding XPath=./*}">
<TextBlock x:Name="nodetext"/>
<HierarchicalDataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=NodeType}"
Value="Element">
<Setter TargetName="nodetext" Property="Text"
Value="{Binding Path=Name}" />
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<Button Content="loadData" HorizontalAlignment="Left" Height="31" Margin="10,246,0,0" VerticalAlignment="Top" Width="63" Click="Button_Click"/>
<ListBox x:Name="listBoxKonsole" HorizontalAlignment="Left" Height="214" Margin="10,27,0,0" VerticalAlignment="Top" Width="752" SelectionChanged="listBoxKonsole_SelectionChanged" />
<TreeView x:Name="treeView" Height="214" Margin="10,282,0,0" Width="752" ItemTemplate="{StaticResource NodeTemplate}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemsSource="{Binding}"
VirtualizingStackPanel.IsVirtualizing="False"
VirtualizingStackPanel.VirtualizationMode="Standard" />
</Grid>
</Window>
和这段代码:
public void listBoxKonsole_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Get the file's location.
string filename = listBoxKonsole.SelectedItem.ToString();
// Make an XmlDataProvider that uses the file.
XmlDataProvider provider = new XmlDataProvider();
provider.Source = new Uri(filename, UriKind.Absolute);
provider.XPath = "./*";
// Make the TreeView display the XmlDataProvider's data.
treeView.DataContext = provider;
}
现在我的Pproblem是我还要显示xml属性而不仅仅是XML节点名称。属性可以在一行中与节点的名称,但我不知道如何实现它。
谢谢
答案 0 :(得分:0)
我为这样的XML文档创建了一个TreeView。 XAML:
<Window.Resources>
<DataTemplate x:Key="AttributeTemplate">
<StackPanel Orientation="Horizontal"
Margin="3,0,0,0"
HorizontalAlignment="Center">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="="" />
<TextBlock Text="{Binding Path=Value, Mode=TwoWay}" />
<TextBlock Text=""" />
</StackPanel>
</DataTemplate>
<HierarchicalDataTemplate x:Key="NodeTemplate">
<StackPanel Orientation="Horizontal" Focusable="False">
<TextBlock x:Name="tbName" Text="Root" FontFamily="Consolas" FontSize="8pt" />
<ItemsControl
ItemTemplate="{StaticResource AttributeTemplate}" HorizontalAlignment="Center"
ItemsSource="{Binding Converter={StaticResource AttrConverter}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
<HierarchicalDataTemplate.ItemsSource>
<Binding Path="Elements" />
</HierarchicalDataTemplate.ItemsSource>
<HierarchicalDataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
<Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=FirstNode.NodeType}" Value="Text">
<Setter TargetName="tbName" Property="Text">
<Setter.Value>
<MultiBinding StringFormat="{}{0} = {1}">
<Binding Path="Name"/>
<Binding Path="FirstNode.Value"/>
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
</Window.Resources>
...
<TreeView x:Name="LizenzAnsicht"
ItemsSource="{Binding Path=Root.Elements, UpdateSourceTrigger=PropertyChanged}"
ItemTemplate="{StaticResource ResourceKey=NodeTemplate}"
/>
XML与XDocument(linq)一起使用,只是设置为TreeView的DataSource。