WPF TreeView 不重绘 [英] WPF TreeView not redrawing

查看:27
本文介绍了WPF TreeView 不重绘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WPF 的新手,我希望 TreeView 始终显示展开/折叠图标(节点旁边的三角形),无论节点中是否有项目.

I am new to WPF and I would like TreeView to show expand/collapse icon (the triangle beside node) at all times, regardless whether node has items in it.

为了始终显示它,我为没有项目的节点添加了一个虚拟项目,其结尾类似于以下内容(目前,我想在代码隐藏中执行此操作):

To show it at all times, I add a dummy item for nodes that have no items ending up with something like below (for now, I would like to do this in code-behind):

+ Node 1
- Node 2
 - Dummy Item
+ Node 3

进一步的要求是一旦扩展了它的节点,就删除它.
为此,我删除了 OnExpand 中的项目:

Further requirement is to delete Dummy Item once the node having it is expanded.
To do that, I remove the item in OnExpand:

public void OnExpand(object sender, EventArgs e)
{
    ...
    foreach (var item in tvItems){
        if (item is dummy){
            tvItems.Children.Remove(item);
        }
    }
    ...
}

这个问题是,一旦节点展开,我看到空行

The problem with this is that once node is expanded, I see empty line

+ Node 1
- Node 2
          <-- How to remove this line?
+ Node 3

如何删除这一行,使列表显示如下:

How do I remove this line so that list shows like:

+ Node 1
  Node 2  // there is no empty line btw Node 2 and Node 3
+ Node 3

推荐答案

最简单的方法是覆盖默认模板以禁用指示器的隐藏.不幸的是,你不能用两行 XAML 代码来做到这一点,但这仍然很容易.

The easiest way is to override the default template to disable hiding of indicator. You can't do that with two lines of XAML code, unfortunately, but it's still easy.

首先,您需要获得 默认 TreeView模板 并将其复制到您的资源字典中.如果您没有样式字典,您可以创建一个新字典并将所有内容放在那里(首先是画笔,然后是所有样式).

First, you need to get default TreeView template and copy it to your resource dictionary. If you don't have styles dictionary, you can create new one and put everything there (brushes first, then all the styles).

其次,您需要找到隐藏按钮的触发器并将其删除(或更改为 IsEnabled 或任何您想要的):

Second, you need to find the trigger which hides the button and remove it (or change to IsEnabled or whatever you want):

<Trigger Property="HasItems"
         Value="false">
    <Setter TargetName="Expander"
            Property="Visibility"
            Value="Hidden" />
</Trigger>

第三,我们需要给主样式我们的自定义键,这样 x:Key="{x:Type TreeView}" 变成 x:Key="CustomTreeViewStyle"代码>例如.并将其用于我们的 TreeView:

Third, we need to give the main style our custom key, so that x:Key="{x:Type TreeView}" becomes x:Key="CustomTreeViewStyle" for example. And use it for our TreeView:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyTreeViewStyles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>


<Grid>
    <TreeView Style="{StaticResource CustomTreeViewStyle}" />
</Grid>

就是这样.不是最短的解决方案,而是一个简单的解决方案,您可以按自己喜欢的方式对其进行自定义.而且您不需要创建幻像项目.

That's it. Not the shortest solution, but an easy one and you can customize it any way you like. And you don't need to create phantom items.

您可以在 App.xaml 中引用此词典,以便每个页面都可以使用它(如果需要).另外,我在这里使用了 MergedDictionary,以防您在页面中已经有一些资源 - 它们将进入 ResourceDictionary 本身.

You can reference this dictionary in App.xaml so every page can use it (if needed). Also, I used MergedDictionary here in case you already have some resource in the page - they will go in ResourceDictionary itself.

这篇关于WPF TreeView 不重绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆