使用TreeView在WPF中获取XML属性 [英] Get XML Attributes in WPF with a TreeView
问题描述
我正在尝试从XDocument在WPF中创建树视图. 到目前为止,我可以创建具有所有节点和值的树. 现在,我想添加所有属性.我的问题从这里开始;-)
I am trying to create a treeview in WPF from an XDocument. So far I can create the tree with all nodes and values. Now I would like to add all attributes. Here my problems start ;-)
相关的XAML如下所示:
The relevant XAML looks like this:
<Window.Resources>
<HierarchicalDataTemplate x:Key="NodeTemplate">
<StackPanel Orientation="Horizontal" Focusable="False">
<TextBlock x:Name="tbName" Text="dummy" />
<ItemsControl ItemsSource="{Binding Attributes}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Name}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</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>
...
<ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<TreeView x:Name="LizenzAnsicht"
ItemsSource="{Binding Path=Root.Elements}"
ItemTemplate="{StaticResource ResourceKey=NodeTemplate}"
/>
</ScrollViewer>
加载XDocument的代码如下:
The Code behind loading the XDocument looks like this:
LadeDatei.LizenzProjekt = XDocument.Load(@"C:\Users\Bernd\Documents\trash\theXMLFile.xml");
LizenzAnsicht.DataContext = LadeDatei.LizenzProjekt;
如前所述,除了没有显示任何属性外,树形结构看起来还不错. Visual Studio在Direktfenster(直接窗口?)中显示了一些错误:
As said the tree structure looks quite good except that no attributes show up. Visual Studio show some errors in the Direktfenster (direct windows?):
System.Windows.Data错误:40:BindingExpression路径错误:在对象""XElement"(HashCode = 51812368)'上找不到"Attributes"属性. BindingExpression:Path =属性; DataItem ='XElement'(HashCode = 51812368);目标元素是'ItemsControl'(Name ='');目标属性为"ItemsSource"(类型为"IEnumerable")
System.Windows.Data Error: 40 : BindingExpression path error: 'Attributes' property not found on 'object' ''XElement' (HashCode=51812368)'. BindingExpression:Path=Attributes; DataItem='XElement' (HashCode=51812368); target element is 'ItemsControl' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
有人暗示我需要做些什么才能使其正常工作吗?
Any hints what I need to do to get it working?
问候 伯恩德
推荐答案
Attributes()
是XElement的一种方法.不支持绑定到方法.
Attributes()
is a method of XElement. Binding to methods is not supported.
我将使用转换器从XElement获取属性以进行绑定:
I would use a converter to get attributes from XElement for binding purpose:
public class XAttributesConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var xe = value as XElement;
if (xe == null)
return Enumerable.Empty<XAttribute>();
return xe.Attributes();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
和
<Window.Resources>
<wpfDemos:XAttributesConverter x:Key="AttrConverter"/>
<Window.Resources>
<ItemsControl ItemsSource="{Binding Converter={StaticResource AttrConverter}}">
这篇关于使用TreeView在WPF中获取XML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!