Extending Checked event in RadTreeView control - how to temporarily remove event handlers?

6 posts, 0 answers
  1. Domagoj
    Domagoj avatar
    17 posts
    Member since:
    Jun 2011

    Posted 29 Jul 2011 Link to this post

    There is this Telerik's RadTreeView (Silverlight Version) control I'd like to extend (inherit from). Let's call the new control MyTreeView.

    The thing I'd like to achieve is that when an item is checked in MyTreeView, it will automatically check all its children items. Also, if after checking the node its parent has all its children checked, I'd like to check the parent too. That would go recursively throughout the tree up to the root. I seem to be unable to find this feature by default.

    I don't need a solution for the recursive algorithm that would check the items, I can manage that easily myself. What I see as a problem is the fact that a user can subscribe on the Checked and Unchecked events. So if a user has subscribed to a Checked event, whenever an item is checked all its children will be checked, thus raising an event for each children node that was automatically checked as a consequence of checking its parent. In my scenario, this is not a desirable behaviour. I want the event to fire only for the manually checked item, not for any other node that will be automatically checked as a consequence.

    I thought I could override the OnChecked method but it is either private or there is no such a method, so I have no access to it. I could add another event handler to the Checked event in a constructor, that would be called before any other event handlers subscribed to an event, but that does not prevent the children nodes from raising the event. I would like to be able to unsubscribe the handlers on the Checked temporarily, then resubscribe them again after checking the nodes. However, since I don't know who is subscribed to an event, I can't unsubscribe them manually (the event is public) and I don't seem to be able to get the invocation list.

    Is there a way to iterate through the subscribed events or any other ways to turn them off temporarily? Is there any better approach to the problem described?

  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 03 Aug 2011 Link to this post

    Hi Domagoj,

    If I understand your scenario correctly you can take advantage of the RadTreeView control Tri-State mode feature. You can find more info about it here. Turning it on will implement the described functionality - the CheckState property of each item will depend on the CheckState property of its child items. Moreover, in this mode when you check a parent item - its children items will be automatically checked. You can examine its functionality in this demo.

    Please give ti a try and let me know if this is what you had in mind.

    Greetings,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  3. DevCraft banner
  4. Domagoj
    Domagoj avatar
    17 posts
    Member since:
    Jun 2011

    Posted 16 Aug 2011 Link to this post

    Actually no, that's not what I had in mind.
    In Tri-State mode, the parent node is set to Indeterminate when some (and not all) of its children are checked (or indeterminate). That's not what I want. I basically want the Two-state with the same functionality - the node is checked if and only if all of it's nodes are checked.

    However, even in your scenario, the problem is with raising events when those items are checked. However, I have found this:

    private void RadTreeViewItem_Checked(object sender, Telerik.Windows.RadRoutedEventArgs e)
    {
        bool isInitiallyChecked = (e as RadTreeViewCheckEventArgs).IsUserInitiated;
    }

    which is helpful in my scenario, as I can prevent the handler from executing if the check event was not user initiated.

    Thanks for the reply. I guess I'll stick with the tri-state.
  5. Anjani
    Anjani avatar
    21 posts
    Member since:
    Mar 2011

    Posted 14 Sep 2012 Link to this post

    Hi Tina,

    I have a query regarding telerik radtreeview. My version of telerik controls is : 2011.1.502.1040 and Im using it in my silverlight project.
    The radtreeview is in tristate extended mode and Im using it with MVVM design pattern and my treeview's itemsource is a runtime collection. So when I check the parent node, all child notes get selected and deselecting the parent node deselects all child nodes.
    Now, I want to extend this functionality in this way :
    Requirement one : out of multiple checked items on treeview, I want to retrieve ONLY the items that have a check on them and NOT the ones that are in intermediate state on sme button click. The radtreeview in its checked item collection gives me all checked items even the ones in intermediate state. So I want only the ones that have a tick mark (ToggleState.On)
    Requirement two :  this is a custom implementation and Im not sure if Telerik supports it. When parent node is selected, all child nodes get selected. Now, when the paent node is clicked the 1st time, all child nodes should get deselected but parent node should remain selected. When the parent node is clicked the 2nd time, it should deselect the parent node. Is this possible will telerik radtreeview ?

    I hope I have conveyed my requirement in the best possible way..
    Awaiting your response
    Anjani
  6. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 19 Sep 2012 Link to this post

    Hello Anjani,

    In an MVVM implementation, we recommend creating the 'tri-state' logic in the view models. Basically the RadTreeView 'tri-state' logic is designed to work in declaratively populated RadTreeView, where both the ItemsSource  and Items collections contain RadTreeViewItems.

    I believe that in your case creating the 'tri-state' logic in the view models will allow you to track the checked items properly and you will also be able to customize the "uncheck an item" logic to leave a parent checked while unchecking its children. Please take a look at this HowTo article - at the bottom you'll find a link to a code library where a sample solution is attached. It should get you started on your implementation.

    If you encounter any issues while building your solution, please let us know.

    Kind regards,
    Tina Stancheva
    the Telerik team

    Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

  7. Anjani
    Anjani avatar
    21 posts
    Member since:
    Mar 2011

    Posted 20 Sep 2012 Link to this post

    Thanks Tina for your response. Incase of issues, I shall get back to you.

    Anjani !
Back to Top
DevCraft banner