简体   繁体   English

如何在 treeViewItem 周围添加边框,包括箭头 WPF/C#

[英]how to add border around treeViewItem, including the arrow WPF/C#

I am trying to change the design of a TreeView, so that I add a border around every TreeViewItem.我正在尝试更改 TreeView 的设计,以便在每个 TreeViewItem 周围添加边框。 As you may know, if I add a border to a TreeViewItem, like so您可能知道,如果我向 TreeViewItem 添加边框,就像这样

<TreeView Name="treeView">
    <TreeView.ItemContainerStyle>
    <Style TargetType="TreeViewItem">
      <Setter Property="IsExpanded" Value="true">
      </Setter>
        <Setter Property="BorderBrush" Value="Green"></Setter>
        <Setter Property="BorderThickness" Value="2,2,2,2" />
     </Style>
  </TreeView.ItemContainerStyle>
  <TreeView.ItemTemplate>
      <-- my template -->
  </TreeView.ItemTemplate>
</TreeView>

the border won't be around the arrow, it will be like this:边框不会围绕箭头,它会是这样的:

treeViewItem 边框的工作原理

What I want to do is something which looks like in the pic below:我想要做的是如下图所示的内容:

在此处输入图片说明

How could I achieve this?我怎么能做到这一点? Is it even possible?甚至有可能吗?

Thank you.谢谢你。

I've managed to make something, which is quite laughable really but it's all i could come up with... I read about ItemPresenter and ControlTemplate, I think it could be done using that, but I found the Expander class to be a little confusing, especially since I had third level children and I was unable to find something relevant about making the Expander work for them.我设法做出了一些东西,这真的很可笑,但这就是我所能想到的......我读过关于 ItemPresenter 和 ControlTemplate,我认为可以使用它来完成,但我发现 Expander 类有点令人困惑,尤其是因为我有第三级孩子,我无法找到有关让 Expander 为他们工作的相关信息。 So, my solution was to make a template, in which I had created a grid consisting of two rows: the first one being of normal height, the second one being a rectangle with the height of 1 and -160 margin (to compensate for the indent).所以,我的解决方案是制作一个模板,在其中我创建了一个由两行组成的网格:第一行是正常高度,第二行是高度为 1 和 -160 边距的矩形(以补偿缩进)。

<StackPanel Background="Transparent" Margin="20,20,20,20">
    <Border BorderThickness="1" BorderBrush="Gray" Margin="0">
        <TreeView Name="treeView" BorderThickness="0" Background="Transparent" Height="400" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <TreeView.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
                </Style>
            </TreeView.ItemContainerStyle>
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="30"/>
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Border Grid.Column="0" Grid.Row="0">
                            <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Click="CheckBox_Click" VerticalAlignment="Center">
                                <TextBlock Text="{Binding Description}" Width="250" Margin="0,0,0,0"/>
                            </CheckBox>
                        </Border>
                        <Rectangle Grid.Row="1" Grid.Column="0"  HorizontalAlignment="Stretch" Fill="Gray" Height="1" Margin="-160"/>
                    </Grid>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Border>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM