WPF TreeView数据绑定隐藏/显示展开/折叠图标 [英] WPF TreeView databinding to hide/show expand/collapse icon

查看:1431
本文介绍了WPF TreeView数据绑定隐藏/显示展开/折叠图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施了一个WPF按需加载树视图,如这个在上述解决方案中,使用一个虚拟元素来保留展开 + 图标/ treeview项目行为。我们要通过添加一个属性来修改模型:

我想通过添加一个属性来修改模型。 HasChildren {get {...}} 给我的支持 TreeNodeViewModel



strong>问题:

如何绑定此属性以隐藏/显示展开图标(在XAML中)?我无法找到合适的触发器/设置器组合。

(INotifyPropertyChanged已正确实现)



感谢您的时间。 >

更新1:

我想使用我的属性 public bool HasChildren em>使用虚拟元素。

确定项目是否有子项是有代价的,但比提取子代还要便宜。

解决方案

朱利安,



这是一个很好的问题。为什么不尝试编写自己的树视图项目? :)我的意思是,不是从零开始,只是从现有的TreeViewItem派生并添加你的属性。我已经准备了一个快速的样本,但随意修改它,如果你愿意(并提出问题,如果有些东西不是很清楚)。这里我们去:

  public class TreeViewItem_CustomControl:TreeViewItem 
{
static TreeViewItem_CustomControl()
{
HasChildrenProperty = DependencyProperty.Register(HasChildren,typeof(Boolean),typeof(TreeViewItem_CustomControl));
}

static DependencyProperty HasChildrenProperty;

public Boolean HasChildren
{
get
{
return(Boolean)base.GetValue(HasChildrenProperty);
}

set
{
if(value)
{
if(this.Items!= null)
{
this.Items.Add(String.Empty); // Dummy item
}
}
else
{
if(this.Items!= null)
{
this.Items。明确();
}
}

base.SetValue(HasChildrenProperty,value);
}

}
}

您的自定义TreeViewItem的代码。现在让我们在XAML中使用它:

 < TreeView> 
< TreeViewItem Header =qwer>
Regulat树视图项。
< / TreeViewItem>
< CustomTree:TreeViewItem_CustomControl x:Name =xyzHeader =temp headerHeight =50>
< TreeViewItem>自定义树视图项,将被删除。< / TreeViewItem>
< / CustomTree:TreeViewItem_CustomControl>
< / TreeView>

如您所见,第一个项目是常规项目,第二个是您的自定义项目。请注意,它有一个孩子。接下来,您可以将 HasChildren 属性绑定到ViewModel中的某个布尔对象,或者只需通过将HasChildren从上述XAML后面的代码设置为 False 来简单地测试自定义类:

  xyz.HasChildren = false; 

现在,尽管你的元素有一个孩子,但是展开按钮不显示,所以这意味着我的



我希望我能帮到您,但随时问你是否有任何疑问。



Piotr。


I implemented a WPF load-on-demand treeview like described in this (very good) article. In the mentioned solution a dummy element is used to preserve the expand + icon / treeview item behavior. The dummy item is replaced with real data, when the user clicks on the expander.

I want to refine the model by adding a property public bool HasChildren { get { ... } } to my backing TreeNodeViewModel.

Question:
How can I bind this property to hide/show the expand icon (in XAML)? I am not able to find a suitable trigger/setter combination.
(INotifyPropertyChanged is properly implemented.)

Thanks for your time.

Update 1:
I want to use my property public bool HasChildren instead of using the dummy element.
Determining whether or not an item has children is somewhat costly, but still much cheaper than fetching the children.

解决方案

Julian,

this is a really good question. Why don't you try to write your own tree view item? :) I mean, not from the scratch, just derive from existing TreeViewItem and add your property. I have prepared a quick sample, but feel free to modify it as you wish (and ask questions if something is not perfectly clear). here we go:

public class TreeViewItem_CustomControl : TreeViewItem
{
    static TreeViewItem_CustomControl()
    {
        HasChildrenProperty = DependencyProperty.Register("HasChildren", typeof(Boolean), typeof(TreeViewItem_CustomControl));
    }

    static DependencyProperty HasChildrenProperty;

    public Boolean HasChildren
    {
        get
        {
            return (Boolean)base.GetValue(HasChildrenProperty);
        }

        set
        {
            if (value)
            {
                if (this.Items != null)
                {
                    this.Items.Add(String.Empty); //Dummy item
                }
            }
            else
            {
                if (this.Items != null)
                {
                    this.Items.Clear();
                }
            }

            base.SetValue(HasChildrenProperty, value);
        }

    }
}

This was the code for your custom TreeViewItem. Now let's use it in XAML:

<TreeView>
    <TreeViewItem Header="qwer">
        Regulat tree view item.
    </TreeViewItem>
    <CustomTree:TreeViewItem_CustomControl x:Name="xyz" Header="temp header" Height="50">
        <TreeViewItem>Custom tree view item, which will be removed.</TreeViewItem>
    </CustomTree:TreeViewItem_CustomControl>
</TreeView>

As you can see, first item is a regular one and the second is your custom one. Please note, that it has one child. Next, you can bind HasChildren property to some Boolean object in your ViewModel or just simply test my custom class by setting HasChildren to False from code behind the above XAML:

xyz.HasChildren = false;

Now, despite your element has one child, expand button is not displayed, so it means, that my custom class works.

I hope I helped you, but feel free to ask if you have any questions.

Piotr.

这篇关于WPF TreeView数据绑定隐藏/显示展开/折叠图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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