Toggle CheckBox on ItemSelect

5 posts, 1 answers
  1. Patrick
    Patrick avatar
    9 posts
    Member since:
    Sep 2010

    Posted 03 Sep 2010 Link to this post

    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

     

     

  2. Patrick
    Patrick avatar
    9 posts
    Member since:
    Sep 2010

    Posted 03 Sep 2010 Link to this post

    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
  3. DevCraft banner
  4. Answer
    Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 07 Sep 2010 Link to this post

    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
  5. Patrick
    Patrick avatar
    9 posts
    Member since:
    Sep 2010

    Posted 07 Sep 2010 Link to this post

    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
  6. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 09 Sep 2010 Link to this post

    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
Back to Top
DevCraft banner