c#wpf样式化HierarchicalDataTemplate表示为键值对链接

时间:2018-10-10 15:07:52

标签: c# wpf styles hierarchicaldatatemplate

我在WPF中将XML表示为树状视图。节点使用HierarchicalDataTemplate设置样式,以表示Xml属性及其值,如下所示。

Normal Node Representation

Selected Node Representation

<HierarchicalDataTemplate DataType="Field" ItemsSource="{Binding XPath=./*}">
    <StackPanel Orientation="Horizontal" Margin="2"  >
        <Border BorderBrush="White" Background="{DynamicResource AccentColorBrush}" BorderThickness="2" CornerRadius="3" Margin="2,0,0,0">
            <TextBlock   Foreground="White"   Margin="3" Text="Field" />
        </Border>
        <Border BorderBrush="White" Background="WhiteSmoke" BorderThickness="2" CornerRadius="3" Margin="2,0,0,0">
            <TextBlock  Margin="3" Text="{Binding XPath=@FieldName}" />
        </Border>
    </StackPanel>
</HierarchicalDataTemplate>

我希望将它们表示为标记/键值对,并以某种链接/等价方式表示,如下图enter image description here 如何通过xaml更改实现此目标。除了在一对之间引入“链接”图像以将其显示为链接之外,我还需要其他方法。

1 个答案:

答案 0 :(得分:1)

您是否正在寻找类似的东西?

enter image description here

如果是这样,您可以使用Separator

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Border Grid.Column="0" BorderBrush="Black" Background="Green" BorderThickness="2" 
            CornerRadius="3" Margin="2,0,0,0">
        <TextBlock VerticalAlignment="Center" Foreground="White" Margin="10,0,10,0" Text="Field" />
    </Border>
    <Border Grid.Column="2" BorderBrush="Black" Background="LightGray" BorderThickness="2" 
            CornerRadius="3" >
        <TextBlock Margin="10,0,10,0" Text="Test1" />
    </Border>
    <Border Grid.Column="1" Margin="-6" Background="White" Height="5" CornerRadius="2">
        <Separator Background="Black" Margin="2,0,2,0"/>
    </Border>
</Grid>