【问题标题】:Get XML Attributes in WPF with a TreeView使用 TreeView 在 WPF 中获取 XML 属性
【发布时间】:2018-09-22 02:53:34
【问题描述】:

我正在尝试从 XDocument 在 WPF 中创建树视图。 到目前为止,我可以创建具有所有节点和值的树。 现在我想添加所有属性。我的问题从这里开始;-)

相关的 XAML 如下所示:

<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 的代码如下所示:

LadeDatei.LizenzProjekt = XDocument.Load(@"C:\Users\Bernd\Documents\trash\theXMLFile.xml");
LizenzAnsicht.DataContext = LadeDatei.LizenzProjekt;

如前所述,树结构看起来相当不错,只是没有显示任何属性。 Visual Studio 在 Direktfenster 中显示一些错误(直接窗口?):

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“XElement”(HashCode=51812368)上找不到“属性”属性。绑定表达式:路径=属性; DataItem='XElement' (HashCode=51812368);目标元素是'ItemsControl'(名称='');目标属性是“ItemsSource”(类型“IEnumerable”)

任何提示我需要做什么才能让它工作?

问候 伯恩德

【问题讨论】:

  • Elements 绑定如何工作?我不明白。 Elements()Attributes() 是 XELement 的方法
  • 我现在不知道为什么它与 Elements 一起使用,但确实如此。由于这行得通,我希望(认为)属性也可以工作......

标签: c# wpf xaml treeview linq-to-xml


【解决方案1】:

Attributes() 是 XElement 的一个方法。不支持绑定到方法。

我会使用转换器从 XElement 获取属性以进行绑定:

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}}">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多