WPF在treeview中使用触发器隐藏项目

WPF在treeview中使用触发器隐藏项目,wpf,triggers,treeview,datatrigger,Wpf,Triggers,Treeview,Datatrigger,我有一个TreeView,其中我使用了一个hierarchycaldatatemplate。有了它,我可以给不同的物品上色(都是同一类型的) 单击页面中的复选框,我想隐藏一些项目(具有特定属性)。我已经测试了很多代码,但是没有一个能正常工作。我在寻找答案 以下是我的代码示例: public partial class Window1 : Window { public Window1() { InitializeComponent(); Tree

我有一个
TreeView
,其中我使用了一个
hierarchycaldatatemplate
。有了它,我可以给不同的物品上色(都是同一类型的)

单击页面中的
复选框
,我想隐藏一些项目(具有特定属性)。我已经测试了很多代码,但是没有一个能正常工作。我在寻找答案

以下是我的代码示例:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        TreeElements tRoot = new TreeElements("Root", false);
        TreeElements t1 = new TreeElements("Node 1", false);
        TreeElements t11 = new TreeElements("Node 1-1", true);
        t1.Children.Add(t11);
        TreeElements t12 = new TreeElements("Node 1-2", false);
        t1.Children.Add(t12);
        tRoot.Children.Add(t1);
        TreeElements t2 = new TreeElements("Node 2", false);
        TreeElements t21= new TreeElements("Node 2-1", false);
        TreeElements t211 = new TreeElements("Node 2-1-1", false);
        t21.Children.Add(t211);
        t2.Children.Add(t21);
        tRoot.Children.Add(t2);
        trv.Items.Add(tRoot);
    }
}

public class TreeElements
{
    public string Description { get; set; }
    public List<TreeElements> Children { get; set; }

    public TreeElements(string description, bool error)
    {
        Description = description;
        _error = error;
        Children = new List<TreeElements>();
    }

    private bool _error;

    public bool Error
    {
        get
        {
            bool bValue = _error;
            foreach (TreeElements child in Children)
                bValue = bValue || child.Error;
            return bValue;
        }
    }
}
公共部分类窗口1:窗口
{
公共窗口1()
{
初始化组件();
TreeElements tRoot=新的TreeElements(“根”,假);
树元素t1=新树元素(“节点1”,false);
树元素t11=新树元素(“节点1-1”,真);
t1.儿童。添加(t11);
树元素t12=新树元素(“节点1-2”,false);
t1.儿童。添加(t12);
tRoot.Children.Add(t1);
树元素t2=新树元素(“节点2”,false);
树元素t21=新树元素(“节点2-1”,false);
树元素t211=新树元素(“节点2-1-1”,false);
t21.儿童。添加(t211);
t2.儿童。添加(t21);
tRoot.Children.Add(t2);
trv.项目。添加(tRoot);
}
}
公共类树元素
{
公共字符串说明{get;set;}
公共列表子项{get;set;}
公共树元素(字符串描述,布尔错误)
{
描述=描述;
_错误=错误;
Children=新列表();
}
私有布尔误差;
公共布尔错误
{
得到
{
布尔值=_错误;
foreach(儿童中的三要素儿童)
bValue=bValue | |子项错误;
返回b值;
}
}
}
以及XAML:

<Window x:Class="WpfApplication2.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <HierarchicalDataTemplate x:Key="HDT_items" ItemsSource="{Binding Path=Children}">
            <TextBlock Text="{Binding Path=Description}" x:Name="txt" />
            <HierarchicalDataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=Error}" Value="true">
                    <Setter TargetName="txt" Property="Foreground" Value="Red" />
                </DataTrigger>
            </HierarchicalDataTemplate.Triggers>
        </HierarchicalDataTemplate>    
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <CheckBox x:Name="chk">Mask some items</CheckBox>

        <TreeView Grid.Row="1" x:Name="trv" ItemTemplate="{StaticResource HDT_items}" />
    </Grid>
</Window>

遮罩一些物品

选中该复选框时,我希望不显示错误为false的节点,但不更改数据源。是否可能,以及如何更改?将TreeElement.Children的类型从List更改为ObservableCollecton。不要在视图中隐藏项目,只需从ViewModel的基础集合中删除tme即可。

将TreeElement.Children的类型从List更改为ObservableCollecton。不要在视图中隐藏项目,只需从ViewModel的基础集合中删除tme即可。

是的,谢谢,它与重建基础集合的函数一起工作。是的,谢谢,它与重建基础集合的函数一起工作。