2

我尝试绑定XDocumentTreeView控制。除属性外,一切正常。#%*!$#@^% 不想出现:D

请帮我修改该代码以使其工作:

<SolidColorBrush x:Key="xmlValueBrush" Color="Blue" />
<SolidColorBrush x:Key="xmAttributeBrush" Color="Red" />
<SolidColorBrush x:Key="xmlTagBrush" Color="DarkMagenta" />
<SolidColorBrush x:Key="xmlMarkBrush" Color="Blue" />

<DataTemplate x:Key="AttributeTemplate">
    <StackPanel Orientation="Horizontal"
            Margin="3,0,0,0"
            HorizontalAlignment="Center">
        <TextBlock Text="{Binding Path=Name}"
             Foreground="{StaticResource xmAttributeBrush}" FontFamily="Consolas" FontSize="8pt" />
        <TextBlock Text="=&quot;"
             Foreground="{StaticResource xmlMarkBrush}" FontFamily="Consolas" FontSize="8pt" />
        <TextBlock Text="{Binding Path=Value, Mode=TwoWay}"
             Foreground="{StaticResource xmlValueBrush}" FontFamily="Consolas" FontSize="8pt" />
        <TextBlock Text="&quot;"
             Foreground="{StaticResource xmlMarkBrush}" FontFamily="Consolas" FontSize="8pt" />
    </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 Path=Attributes}">
            <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="Text">
            <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Value, Mode=TwoWay}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
            <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}" />
        </DataTrigger>
    </HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>

我的TreeView

<TreeView x:Name="XmlTree" Grid.Row="1"
      ItemsSource="{Binding Path=Root.Elements, UpdateSourceTrigger=PropertyChanged}"
      ItemTemplate="{StaticResource NodeTemplate}"
      SelectedItemChanged="XmlTree_SelectedItemChanged" />

这是我背后的代码:

private void BindXmlData(string filePath)
{
    _xml = XDocument.Load(filePath);
    XmlTree.DataContext = _xml;
}

所有节点都显示良好,但我无法管理属性以使其可见

4

2 回答 2

2

您可以从以下位置更改您的 DataTriggers 部分:

            <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}" />
            </DataTrigger>

到:

            <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name.LocalName}" />
            </DataTrigger>

希望能有所帮助,谢谢!- 沙姆斯

于 2014-04-23T18:37:12.513 回答
0

由于答案对我不起作用,我想向 Ashs Answer 提示(Get XML Attributes in WPF with a treeview)。

在此处给出的原始代码中,我发现了另一个问题。节点的值不会呈现在树视图中。为此,我需要删除第一个 DataTrigger 并添加另一个。所以这部分

<HierarchicalDataTemplate.Triggers>
...
</HierarchicalDataTemplate.Triggers>

看起来像这样:

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

现在每个包含 Value 的节点都呈现为: Node-Name = Value

于 2018-04-12T11:18:46.507 回答