取消选择和重新选择 TreeView 中的 TreeViewItem
我遇到了以下问题:
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置 item.IsSelected = false 后;您必须为树视图调用 .Focus() 。
你写的 focus() 并不能解决你的问题。你在哪里调用你的 deselectAll() ?
作为解决方法,您可以使用 MouseLeftDown 将项目设置为选定状态。
PS:不要忘记将答案标记为答案。
after you set item.IsSelected = false; you have to call .Focus() for your treeview.
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.