【问题标题】:WPF TreeView databinding to hide/show expand/collapse iconWPF TreeView 数据绑定隐藏/显示展开/折叠图标
【发布时间】:2011-02-09 18:05:12
【问题描述】:

我实现了一个 WPF 按需加载树视图,如 this(非常好)文章中所述。 在上述解决方案中,一个虚拟元素用于保留展开 + 图标/树视图项目的行为。当用户单击扩展器时,虚拟项目将替换为真实数据。

我想通过将属性public bool HasChildren { get { ... } } 添加到我的支持TreeNodeViewModel 来优化模型。

问题:
如何绑定此属性以隐藏/显示展开图标(在 XAML 中)?我找不到合适的触发器/设置器组合。
(INotifyPropertyChanged 已正确实现。)

感谢您的宝贵时间。

更新 1:
我想使用我的属性public bool HasChildren而不是使用虚拟元素。
确定一个项目是否有孩子有点昂贵,但仍然比获取孩子便宜得多。

【问题讨论】:

    标签: wpf data-binding treeview expand collapse


    【解决方案1】:

    朱利安,

    这是一个非常好的问题。为什么不尝试编写自己的树视图项? :) 我的意思是,不是从头开始,只是从现有的 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.Clear();
                    }
                }
    
                base.SetValue(HasChildrenProperty, value);
            }
    
        }
    }
    

    这是您的自定义 TreeViewItem 的代码。现在让我们在 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>
    

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

    xyz.HasChildren = false;
    

    现在,尽管您的元素有一个子元素,但未显示展开按钮,这意味着我的自定义类有效。

    希望对您有所帮助,但如果您有任何问题,请随时提问。

    彼得。

    【讨论】:

      【解决方案2】:

      在快速查看 Josh 的代码后,我发现了这个构造函数:

      protected TreeViewItemViewModel(TreeViewItemViewModel parent, bool lazyLoadChildren)
      {
          _parent = parent;
      
          _children = new ObservableCollection<TreeViewItemViewModel>();
      
          if (lazyLoadChildren)
              _children.Add(DummyChild);
      }
      

      因此,如果您从继承的 ViewModel 类中为 lazyLoadChildren 参数传递 false,则不应出现 + 图标,因为未添加 DummyChild。由于您似乎知道您的项目是否有子项,因此您应该能够为 lazyLoadChildren 属性传递适当的值。还是我错过了什么?

      【讨论】:

        猜你喜欢
        • 2013-01-13
        • 1970-01-01
        • 2012-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-15
        相关资源
        最近更新 更多