取消选择和重新选择 TreeView 中的 TreeViewItem

发布于 2024-10-25 07:54:26 字数 482 浏览 4 评论 0原文

我遇到了以下问题:

在我的 TreeView 中,当用户单击 TreeView 但不是 TreeViewItem 时,我通过调用我自己的 deselect() 方法添加了取消选择功能。 这是我的 TreeView 方法:

public void deselectAll()
{
    TreeViewItem item = SelectedItem as TreeViewItem;
    if (item != null)
    {
        this.Focus();
        item.IsSelected = false;
    }
}

我的问题是,在取消选择 TreeViewItem 后我无法重新选择它。我读过,聚焦 TreeView 本身应该可以解决这个问题,但事实并非如此。如果我将“Focus()”放在“IsSelected = false”之前或之后也没关系。

有谁知道为什么这不起作用?任何想法将不胜感激。

I've got the following problem:

In my TreeView i added unselect functionality by calling my own deselect()-method when the user clicks the TreeView but not a TreeViewItem.
Here is my TreeView method:

public void deselectAll()
{
    TreeViewItem item = SelectedItem as TreeViewItem;
    if (item != null)
    {
        this.Focus();
        item.IsSelected = false;
    }
}

My problem is, that i can't reselect a TreeViewItem after i unselected it. I've read, that focusing the TreeView itself should solve this problem, but it's not. It also doesn't matter if i put the 'Focus()' before or after the 'IsSelected = false'.

Does anyone has an idea why this is not working? Any thoughts would be appreciated.

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

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

发布评论

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

评论(1

秋日私语 2024-11-01 07:54:26

设置 item.IsSelected = false 后;您必须为树视图调用 .Focus() 。

        <TreeView MouseLeftButtonDown="TreeView_MouseLeftButtonDown">
            <TreeViewItem Header="Employee1">
                <TreeViewItem Header="Jesper"/>
                <TreeViewItem Header="Aaberg"/>
                <TreeViewItem Header="12345"/>
            </TreeViewItem>
            <TreeViewItem Header="Employee2">
                <TreeViewItem Header="Dominik"/>
                <TreeViewItem Header="Paiha"/>
                <TreeViewItem Header="98765"/>
            </TreeViewItem>
        </TreeView>

    private void TreeView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var tv = sender as TreeView;

        if (tv != null)
        {
            var item = (TreeViewItem)tv.SelectedItem;
            item.IsSelected = false;
            tv.Focus();
        }
    }

你写的 focus() 并不能解决你的问题。你在哪里调用你的 deselectAll() ?

作为解决方法,您可以使用 MouseLeftDown 将项目设置为选定状态。

PS:不要忘记将答案标记为答案。

after you set item.IsSelected = false; you have to call .Focus() for your treeview.

        <TreeView MouseLeftButtonDown="TreeView_MouseLeftButtonDown">
            <TreeViewItem Header="Employee1">
                <TreeViewItem Header="Jesper"/>
                <TreeViewItem Header="Aaberg"/>
                <TreeViewItem Header="12345"/>
            </TreeViewItem>
            <TreeViewItem Header="Employee2">
                <TreeViewItem Header="Dominik"/>
                <TreeViewItem Header="Paiha"/>
                <TreeViewItem Header="98765"/>
            </TreeViewItem>
        </TreeView>

    private void TreeView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var tv = sender as TreeView;

        if (tv != null)
        {
            var item = (TreeViewItem)tv.SelectedItem;
            item.IsSelected = false;
            tv.Focus();
        }
    }

you wrote focus() dont solve your problem. where do you call your deselectAll()?

as a workaround you can use the MouseLeftDown to set an item as selected.

ps: dont forget to mark an answer as an anwser.

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