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

Toggle CheckBox on ItemSelect

4 Answers 100 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Patrick
Top achievements
Rank 1
Patrick asked on 03 Sep 2010, 06:51 PM
Hello,

I would like to toggle the check box of an item in a TreeView by Selecting the item. It makes it much easier for the user to 'check' the item without putting the mouse directly on the tiny checkbox for the item. Currently I have this partially working by using the following code:

private void RadTreeView_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            //This only works when it is a new selection, this event does not fire upon clicking on the already selected item
            Telerik.Windows.Controls.RadTreeViewItem selectedItem = e.Source as Telerik.Windows.Controls.RadTreeViewItem;
              
            if (selectedItem.Level == 1)
            {          
                if (selectedItem.CheckState == ToggleState.Off)
                {
                    selectedItem.CheckState = ToggleState.On;
                }
                else
                {
                    selectedItem.CheckState = ToggleState.Off;
                }
            }  
        }


The only issue I have with the above is if the user clicks on the item already selected nothing happens, the user must click another item OR just click the tiny checkbox. I'm using HierarchicalDataTemplate binding and TriStateMode. Any suggestions?? Thanks, Patrick

 

 

4 Answers, 1 is accepted

Sort by
0
Patrick
Top achievements
Rank 1
answered on 03 Sep 2010, 07:27 PM
If Selected fired on every click of an item, SelectionChanged or not, and contained PreviouslySelected and CurrentlySelected properties that would make it easy to test for in this situation. Thanks

-Patrick
0
Accepted
Hristo
Telerik team
answered on 07 Sep 2010, 04:26 PM
Hi Patrick,

You can try the following approach: register an event handler to the MouseLeftButtonUp event. Then get the selected element and perform the required logic on it. If you need to maintain only one checked item you can store the previous checked one and handle it also.

Following code snippets illustrate what I mean:

<telerik:RadTreeView Name="SampleTreeView" IsTriStateMode="True" IsOptionElementsEnabled="True"
        MouseLeftButtonUp="RadTreeView_MouseLeftButtonUp">
    <telerik:RadTreeViewItem Header="Item 1">
        <telerik:RadTreeViewItem Header="Item 1.1" />
        <telerik:RadTreeViewItem Header="Item 1.2" />
    </telerik:RadTreeViewItem>
    <telerik:RadTreeViewItem Header="Item 2" />
    <telerik:RadTreeViewItem Header="Item 3" />
    <telerik:RadTreeViewItem Header="Item 4" />
    <telerik:RadTreeViewItem Header="Item 5" />
</telerik:RadTreeView>


private void RadTreeView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    RadTreeViewItem itemContainer = SampleTreeView.SelectedContainer;
    if (itemContainer != null)
    {
        if (itemContainer.CheckState == ToggleState.Off)
        {
            itemContainer.CheckState = ToggleState.On;
        }
        else
        {
            itemContainer.CheckState = ToggleState.Off;
        
  
    }
}

Hope this helps. Please let us know if you need more info.

Greetings,
Hristo Milyakov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Patrick
Top achievements
Rank 1
answered on 07 Sep 2010, 05:05 PM
Thank you Hristo that's just what I needed. I was very close and already registered MouseLeftButtonDown on the RadTreeView, but that never seems to get fired no matter how much/where I click, so I gave up. MouseLeftButtonUp does fire as expected and works great. Thank you.

-Patrick
0
Hristo
Telerik team
answered on 09 Sep 2010, 12:31 PM
Hi Patrick,

The MouseLeftButtonDown event is handled by the tree view item (e.Handled = true). That is why your handler is not hit. However you should be able to receive those events if you attach the handler using the AddHandler method (from code behind) with HandledEventsToo parameter set to True.

Kind regards,
Hristo Milyakov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
TreeView
Asked by
Patrick
Top achievements
Rank 1
Answers by
Patrick
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or