WPF 在树视图中隐藏带有触发器的项目

发布于 2024-08-04 21:31:39 字数 2873 浏览 5 评论 0原文

我有一个 TreeView,其中使用 HierarchicalDataTemplate。通过这个,我为不同的物品(所有相同类型)着色。

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

这是我的代码示例:

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;
        }
    }
}

和 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>

我希望,当选中该复选框时,不显示 Error=false 的节点,但不更改数据源。这可能吗?如何实现?

I've got a TreeView, in which I use a HierarchicalDataTemplate. With this, I color my different items (all of the same type).

With a click on a CheckBox in the Page, I want to hide some of the items (with a certain Property). I've tested many code, but nothing works properly. I'm looking for answers ...

Here's a sample of my code :

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;
        }
    }
}

And the 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>

I want, when the checkbox is checked, to not display the nodes who are with Error=false, but without changing the data source. Is it possible, and how ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千秋岁 2024-08-11 21:31:39

将 TreeElement.Children 的类型从 List 更改为 ObservableCollection。并且无需隐藏视图中的项目,只需从 ViewModel 的底层集合中删除 tme 即可。

Change type of TreeElement.Children from List to ObservableCollecton. And instead of hiding items in the view, simply remove tme from underlying collection in the ViewModel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文