在WPF中,你如何编程取消TreeView中的所有项目? [英] In WPF, how do you programmatically deselect all Items in a Treeview?

查看:116
本文介绍了在WPF中,你如何编程取消TreeView中的所有项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个递归方法来取消在WPF TreeView中的所有项目。那复杂的事情的事情是,每一个树型视图不是一个小的TreeView。这将导致你所要做的铸造来回了很多。所以,这里是我曾尝试:

I'm trying to create a recursive method to deselect all items in a WPF TreeView. The thing that complicates things is that each TreeViewItem is not a mini-TreeView. This causes you to have to do a lot of casting back and forth. So, here's what I have tried:

TreeViewDeselectAll(myTreeView.Items);      

// Must send in ItemCollection to allow the recursive call
private void TreeViewDeselectAll(ItemCollection myTreeViewItems)
{
    // The only way to get to the IsSelected property is to turn it back into a TreeViewItem
    foreach (TreeViewItem currentItem in myTreeViewItems)
    {
        currentItem.IsSelected = false;
        if (currentItem.HasItems)
        {
             // Recursify!
             TreeViewDeselectAll(currentItem.Items);
        }
    }
}



有没有人成功地取消所有项目在一个TreeView?你甚至能以递归方式遍历一个TreeView?

Has anyone successfully deselected all items in a TreeView? Have you even been able to traverse a TreeView in a recursive manner?

的WinForms TreeView控件有一个节点集合,这实在是一个小型的TreeView。这允许递归就好了。但WPF的TreeView不具有节点。

The Winforms TreeView has a Nodes collection, which is really a mini-TreeView. This allows recursion just fine. But the WPF TreeView does not have Nodes.

工作在.NET 4.0中。

Working in .Net 4.0.

推荐答案

每个树型视图是一个小型的TreeView。你得到了错误的印象,或者你读的东西错了TreeViewItems。

Each TreeViewItem is a Mini-TreeView. You got the wrong impression or you read something wrong about TreeViewItems.

你所做错了,是通过项目集合这犯规包含类型树型视图的孩子,而是那些数据。项目

What you doing wrong is passing the Items collection which doesnt contain children of type TreeViewItem but instead those are the data items.

通过儿童收集和你应该做的罚款

Pass the children collection and you should do fine.

这样的:

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

    }

    private bool @switch = false;

    private void TreeViewDeselectAll(IEnumerable myTreeViewItems, bool value)
    {
        if (myTreeViewItems != null)
        {
            foreach (var currentItem in myTreeViewItems)
            {
                if (currentItem is TreeViewItem)
                {
                    TreeViewItem item = (TreeViewItem)currentItem;
                    item.IsSelected = value;
                    if (item.HasItems)
                    {
                        TreeViewDeselectAll(LogicalTreeHelper.GetChildren(item), value);
                    }
                }
            }
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.TreeViewDeselectAll(LogicalTreeHelper.GetChildren(this.treeView), this.@switch);
    }
}



XAML是这样的,例如:

XAML would look like this for example:

<StackPanel>
    <TreeView Name="treeView">
        <TreeViewItem Header="First Level">
            <TreeViewItem Header="Second Level"/>
        </TreeViewItem>
    </TreeView>
    <Button Click="Button_Click">select/unselect all</Button>
</StackPanel>



我改变了你的TreeViewDeselectAll方法一点点。基于开关可以选择或取消选择所有。

I changed your TreeViewDeselectAll method a little bit. Based on switch you can select or unselect all.

这篇关于在WPF中,你如何编程取消TreeView中的所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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