我正在搜索数小时,我正在尝试将XML文件绑定到TreeView。通常,我想在ListBox中选择一个XML文件,然后在TreeView中打开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>
非常感谢。如果需要我的整个代码,请随时询问!
编辑:
可以达到此输出:输出
使用此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;
}
现在,我的问题是我还想显示XML属性,而不仅仅是XML节点名称。属性可以与节点的名称列入一条线,但我不知道如何意识到它。
谢谢
我为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的数据源。