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()
{
TreeView开发者_如何学运维Item 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.
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.
精彩评论