How to deselect all selected items in a WPF treeview when clicking on some empty area? -


I have a strange problem with WPF. I have a tree view, and selection of items works fine till now . The problem is, I want to deselect the currently selected item when the user is clicked within the blank area of ​​the tree view. By default, Treviewview selects the current item, and I've added a context menu option to deselect it, which is strict:

  // Note: It starts It is also done repeatedly, so it works for // hair items as well as the tree view. ITEM container generator. ContainerFrematum (Treeview Select Eatam) as TriviaTim). Similarly selected = Incorrect; In addition, this is counter-intuitive because it requires the user to right-click for the first time, and secondly, after deselecting it like this, User can not choose any more item by clicking on it # Commodity how does it work?  

EDIT: MORE INFORMATION: I have added a handler in TreeView to handle the mouse click event, but the sender is always a TreeView example , Even if I click directly on TreeViewItem if I instead of adding a handler to my TreeView.ItemTemplate (i.e. the first child in the template), I have an empty field (which is logical Does not receive events at any time while clicking on) The code looks like this:

  Private Zero MyTreeView_MouseUp (Object Sender, System.Windows.Input.MouseButtonEventArgs e) {If ((Sender as TreeViewItem) == Faucet) {// Always Trigger system Diagnostics. Good. Type ("empty area clicked"); }}  

and for this there is XAML:

   

I found this to work better for me. I check the original sources If it comes to me, then a sculpture will be an image or a text block. I also use a visible object with a hierarchical datatyplette and BasicTreeViewBase is the base class for all my different objects. I here is the code.

  Private Zero TemplateTreeView_MouseDown (Object Sender, MouseButtonEventArgs e) {if (e.ChangedButton == MouseButton.Right & amp; (E.OriginalSource Image) & amp; Amp ;! ( E. The original source is textbox)) {BasicTreeViewBase item = TemplateTreeView.SelectedItem BasicTreeViewBase; If (item! = Null) {TemplateTreeView.Focus (); item. Selected = false; }}}  

Comments