WPF, TreeView bug, can't select root item after item removed from treeview
I have a very weird bug in a three level deep TreeView. It is intermittent and I can't find how to reproduce it consistently. After progra开发者_JAVA百科mmatically removing, adding then removing some third level items, when I click on the root item it isn't selected. It can still expand/collapse but can't be selected with a mouse click and it doesn't fire ItemSelectionChange event. You can click to select a second level or third level items and after that you're finally able to select the root item. I've tried to set IsSelected = false for all items after removing and tried to do a nice clean-up when removing items but it doesn't help. Did anybody run into that bug? Do you have any suggestions to remove this bug?
Happens to me after programatically settings the IsSelected to a node. Then I can't click to select the previous node.
[Edit]
Finally figured out what was going on. Seems it’s not enough to set the IsSelected property, because the tree internal logical focus remains on the old selected element.
The event was sent to the tree item, but because it already had logical focus internally, it didn’t fire the focus change (from which I suspect the tree knows when to change the selected item).
If I call focus on the tree node for which I set IsSelected to true, it works fine.
====================
Do you set IsSelected on true for the new added elements? If so, also do a focus, like this:
DependencyObject nodeVisualContainer = mTreeControl.ItemContainerGenerator.ContainerFromItem(nodeToSelect);
if (nodeVisualContainer is UIElement)
((UIElement)nodeVisualContainer).Focus();
Note: This only does a logical focus on the element. Keyboard focus will not be affected.
精彩评论