This is a migrated thread and some comments may be shown as answers.

How to deselect a RadTreeView when clicking on the body?

3 Answers 552 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 02 Dec 2016, 03:53 PM

I am replacing a TreeView control with a RadTreeView control however I have run in to a issue. I want the user to be able to click on the body of the TreeView (instead of a node) and have it deselect the item in the tree view.

 

With the original TreeView I would do

<TreeView Grid.Row="0" MouseDown="TreeView_OnMouseDown">
    <TreeViewItem Header="Item 1"/>
    <TreeViewItem Header="Item 2"/>
</TreeView>

Then in the code behind I would have

private void TreeView_OnMouseDown(object sender, MouseButtonEventArgs e)
{
    var treeView = (TreeView)sender;
    var items = treeView.Items.Cast<TreeViewItem>();
    foreach (var treeViewItem in items)
    {
        treeViewItem.IsSelected = false;
    }
     
}

 

And this would work correctly. However if I do

<telerik:RadTreeView Grid.Row="1" MouseDown="RadTreeView_OnMouseDown">
    <telerik:RadTreeViewItem Header="Item 3"/>
    <telerik:RadTreeViewItem Header="Item 4"/>
</telerik:RadTreeView>

and

private void RadTreeView_OnMouseDown(object sender, MouseButtonEventArgs e)
{
    var treeView = (RadTreeView)sender;
    treeView.SelectedItem = null;
    MessageBox.Show("RadTreeView_OnMouseDown fired!");
}

the OnMouseDown event never fires.

Looking at the OnMouseDown event with Snoop it appears to be getting marked as handled by the ScrollViewer inside the RadListView.

What is the correct way to deselect the node when clicking on the body of the tree view? Attached is a simple example program that shows the two controls side by side with their different behaviors.

3 Answers, 1 is accepted

Sort by
0
Scott
Top achievements
Rank 1
answered on 02 Dec 2016, 03:55 PM

I can't attach a zip to the post so here is a external link to a zip of a basic project that re-creates the problem.

http://www.filedropper.com/treeviewissue 

0
Accepted
Tanya
Telerik team
answered on 05 Dec 2016, 12:44 PM
Hi Scott,

Thank you for sharing the sample project.

The MouseDown event is handled internally in the code of RadTreeView. You can use the AddHandler() method to subscribe for the event even when it is handled. This is achieved by passing true as a value for the handledEventsToo boolean parameter:
this.radTreeView.AddHandler(RadTreeView.MouseDownEvent, new RoutedEventHandler(this.RadTreeView_OnMouseDown), true);

Since there are some differences in the way RadTreeView handles the events compared with TreeView, you will need to check whether the user has clicked on a RadTreeViewItem or not. Here is a sample showing how you can implement the desired behavior:
private void RadTreeView_OnMouseDown(object sender, RoutedEventArgs e)
{
    FrameworkElement element = e.OriginalSource as FrameworkElement;
    if (element != null)
    {
        RadTreeViewItem container = element.ParentOfType<RadTreeViewItem>();
        if (container == null)
        {
            this.radTreeView.SelectedItems.Clear();
        }
    }
}

Hope this is helpful.

Regards,
Tanya
Telerik by Progress
Telerik UI for WPF is ready for Visual Studio 2017 RC! Learn more.
0
Scott
Top achievements
Rank 1
answered on 05 Dec 2016, 02:44 PM

Thank you, I figured out the AddHandler part myself but I did not think of checking the "ParentOfType".I think this will cause the item to also be unselected if there are many items in the list and you click on the scroll bar, but I think I can do similar checks in the OnMouseDown for that too to not unselect the items.

Tags
TreeView
Asked by
Scott
Top achievements
Rank 1
Answers by
Scott
Top achievements
Rank 1
Tanya
Telerik team
Share this question
or