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

UserChecked event

16 Answers 119 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Rachel Martin
Top achievements
Rank 1
Rachel Martin asked on 15 Oct 2009, 03:29 PM
I'm currently using the RadTreeView's Checked and Unchecked events to perform an action, but I have IsTriStateMode set to True, so when the user checks or unchecks a box, it triggers the appropriate event for all the children and parents as well.  I don't mind if the event gets fired, but I need to only perform my action on the item that the user interacted with (i.e. UserChecked event).  How can I distinguish this?

16 Answers, 1 is accepted

Sort by
0
Rachel Martin
Top achievements
Rank 1
answered on 15 Oct 2009, 05:22 PM
I was able to create what I needed by doing all of the following:

  • Use the PreviewChecked and PreviewUnchecked events instead of the Checked and Unchecked events.
  • Set e.Handled = true in my event handler.
  • When the action is complete, detach from the events, set the CheckState of the RadTreeViewItem, and reattach to the events.

This way my event is only fired on the item that the user interacted with.
0
Meliton Tienda
Top achievements
Rank 1
answered on 06 Jan 2010, 12:01 AM
Hello..
 I have the same problem,,
do you have any sample, please?
0
Valentin.Stoychev
Telerik team
answered on 06 Jan 2010, 06:52 AM
Hello Meliton Tienda,

you can check the OriginalSource property of the event args parameter. It contains the original item that was clicked. More info is available here:
http://www.telerik.com/help/silverlight/radtreeview-features-checkbox-and-lines-support.html

All the best,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Meliton Tienda
Top achievements
Rank 1
answered on 07 Jan 2010, 12:49 AM
thanks for your answer.

But i have other problem..

When i try to use the IsTriStateMode="True" property,  and I clicked one parent item,, all child items are checked.
i need to use the property but when i checked the parent item only some specific child items are checked.

any suggestion for this..

 

0
Rachel Martin
Top achievements
Rank 1
answered on 08 Jan 2010, 02:36 PM
I was able to create what I needed by doing all of the following:

  • Use the PreviewChecked and PreviewUnchecked events instead of the Checked and Unchecked events.
        public TreeViewContainer() 
        { 
            InitializeComponent(); 
 
            // Enable TreeView events 
            SetPreviewCheckedEvents(true); 
 
        } 
 
 
        public void SetPreviewCheckedEvents(bool enable) 
        { 
            if (enable) 
            { 
                TreeView.PreviewChecked += new EventHandler<RadRoutedEventArgs>(TreeView_UserChecked); 
                TreeView.PreviewUnchecked += new EventHandler<RadRoutedEventArgs>(TreeView_UserChecked); 
            } 
            else 
            { 
                TreeView.PreviewChecked -= TreeView_UserChecked; 
                TreeView.PreviewUnchecked -= TreeView_UserChecked; 
            } 
        } 
 

  • Set e.Handled = true in my event handler. (I am binding to the TreeViewDataItem class in my tree, and the CheckState of my tree binds to the CheckState on this class.  If you are not doing this, use RadTreeViewItem for all references to TreeViewDataItem; otherwise use the class you bind to.)
  • When the action is complete, detach from the events, set the CheckState of the RadTreeViewItem, and reattach to the events.
        private void TreeView_UserChecked(object sender, RadRoutedEventArgs e) 
        { 
            // Stop all checked events so children do not perform this action 
            e.Handled = true
 
            // Retrieve change 
            RadTreeViewItem visualTreeItem = (RadTreeViewItem)e.Source; 
            TreeViewDataItem treeItemData = (TreeViewDataItem)visualTreeItem.Item; 
 
            // Update treeItemData by using a separate thread that does not perform a preview so we can 
            // toggle the visible CheckState.
            visualTreeItem.Dispatcher.BeginInvoke( 
                new DelayChecking(DelayedCheck), 
                treeItemData, 
                visualTreeItem.CheckState); 
        } 
 
        protected delegate void DelayChecking(TreeViewDataItem treeItemData, ToggleState checkState, bool isExplicit) 
 
        protected void DelayedCheck(TreeViewDataItem treeItemData, ToggleState checkState) 
        { 
            SetPreviewCheckedEvents(false);     // disable preview 
            treeItemData.CheckState = checkState; 
            SetPreviewCheckedEvents(true);      // enable preview 
        } 
 

0
Miroslav
Telerik team
answered on 12 Jan 2010, 02:03 PM
Hi Meliton Tienda,

Thank you for sharing your solution. I must admit that we have not considered this scenario and currently there is no way to distinguish the initiator of the event.

This is a valid case, we have added this in the TreeView's product backlog for future consideration. 

Your Telerik Points have been updated for your feedback.

Regards,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Francisco Amador
Top achievements
Rank 1
answered on 12 Apr 2010, 08:50 PM
I used Rachel Martin's code to obtain the event initiator. Until recently things were working fine, but when I upgraded to the newest internal build (2010.1.412.1030) I began to experience a rather odd bug. I'm utilizing TriStateMode so I expect that once the parent is in indeterminate mode and it is clicked, it will become enabled along with all it's children. Instead of this, the parent stays in indeterminate mode and it's children's status become toggled.

If you use the code from the How to Retrieve All Checked Items example but change the tree in the xaml to:
<telerikNavigation:RadTreeView x:Name="treeView1" 
                IsLineEnabled="True" ItemsOptionListType="CheckList" IsOptionElementsEnabled="True" IsRootLinesEnabled="True" IsTriStateMode="True" 
                ItemsSource="{StaticResource FamiliesItemsSource}" ItemTemplate="{StaticResource FamilyTemplate}"/> 

And use the following code in the MainPage.xaml.cs
        public MainPage() 
        { 
            InitializeComponent(); 
            SetPreviewCheckedEvents(treeView1, true); 
        } 
 
        public void SetPreviewCheckedEvents(RadTreeView treeView, bool enable) 
        { 
            if (enable) 
            { 
                treeView.PreviewChecked += new EventHandler<RadRoutedEventArgs>(TreeView_UserChecked); 
                treeView.PreviewUnchecked += new EventHandler<RadRoutedEventArgs>(TreeView_UserChecked); 
            } 
            else 
            { 
                treeView.PreviewChecked -= TreeView_UserChecked; 
                treeView.PreviewUnchecked -= TreeView_UserChecked; 
            } 
        } 
 
        private void TreeView_UserChecked(object sender, RadRoutedEventArgs e) 
        { 
            // Stop all checked events so children do not perform this action  
            e.Handled = true
 
            // Retrieve change  
            RadTreeViewItem visualTreeItem = (RadTreeViewItem)e.Source; 
 
            // Update treeItemData by using a separate thread that does not perform a preview so we can  
            // toggle the visible CheckState. 
            visualTreeItem.Dispatcher.BeginInvoke( 
                new DelayChecking(DelayedCheck), 
                visualTreeItem, 
                visualTreeItem.CheckState); 
        } 
 
        private delegate void DelayChecking(RadTreeViewItem treeItemData, ToggleState checkState); 
 
        private void DelayedCheck(RadTreeViewItem treeItemData, ToggleState checkState) 
        { 
            SetPreviewCheckedEvents(treeItemData.ParentTreeView, false);     // disable preview  
            treeItemData.CheckState = checkState; 
            SetPreviewCheckedEvents(treeItemData.ParentTreeView, true);      // enable preview  
        } 

To recreate the bug using the code above and the internal build mentioned above just expand the first item and select a child. After that simply click the parent that is now in indeterminate mode.

Please let me know if you have any other way to fire an event only for the event initiator.

Thank you.
0
Valentin.Stoychev
Telerik team
answered on 16 Apr 2010, 03:02 PM
Hello Francisco Amador,

I tried to replicate the problem, but was not able to.

Please see the attached project and let us know what we are missing from the setup.

Sincerely yours,
Valentin.Stoychev
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
Francisco Amador
Top achievements
Rank 1
answered on 16 Apr 2010, 05:38 PM
That solution does indeed have all that is necessary to produce the bug.

Steps to reproduce:
  1. Run application and expand the first container (Simpsons)
  2. Click on the checkbox for the first child (Homer). The parent's status will now become Indeterminate.
  3. Click on the checkbox for the parent (Simpsons). Here the bug becomes visible.

Once step 3 is done, the status of the tree will go from:
Simpsons - Indeterminate
Homer - On
Marge - Off
Bart - Off
Lisa - Off
Maggie - Off

To the following status:

Simpsons - Indeterminate
Homer - Off
Marge - On
Bart - On
Lisa - On
Maggie - On

The expected result is:
Simpsons - On
Homer - On
Marge - On
Bart - On
Lisa - On
Maggie - On

Setting a breakpoint inside the DelayedCheck function I did notice that it is being called more times than it should when clicking on the parent while in the indeterminate state. It actually calls once for the parent, then twice for each child that was checked. The first time that it calls it for the child it sets the child from On to Off, then it call it again to set it from Off to Off. I thought that putting the e.Handled in the PreviewChecked function would stop the event from propagating to others.

The code is in fact removing the events, then setting the checked status of the item, and then reenabling the events. I don't see why it is running the event for some of the children.

0
Miro Miroslavov
Telerik team
answered on 21 Apr 2010, 08:23 AM
Hi Francisco Amador,

Please find the attached project. Actually if you don't write any code, the RadTreeView will behave exactly as you need.
Hope will works for you.

Kind regards,
Miro Miroslavov
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
Francisco Amador
Top achievements
Rank 1
answered on 21 Apr 2010, 02:23 PM
Yes, I understand that the bug goes away when I don't run the custom logic, but that custom logic is the one that allows me to run an event only for the event initiator in TriStateMode. Since I cannot obtain the event initiator through built in ways in the Telerik controls, this is the only way that I have found to do so. I guess I will just continue to use the logic and live with the bug until someone finds another way to fire an event only for the event initiator when in TriStateMode. So far, I have not been able to come up with a better way.

I see that this is in the Public Issue Tracking System Here so I will just vote for it and begin tracking it for updates.

Thank you for your response,
Francisco Amador

edit: Changed link to PITS to link to public version of PITS
0
Valentin.Stoychev
Telerik team
answered on 22 Apr 2010, 09:08 AM
Hello Francisco Amador,

Yes - based on the feedback we decided that it will be better if we add the event initator as a property of the event args. Stay tuned and track the PITS item - it should be done in the next 2-3 weeks as part of the internal builds.

Best wishes,
Valentin.Stoychev
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
Francisco Amador
Top achievements
Rank 1
answered on 09 Jun 2010, 04:37 PM
Hello,
I was just wondering if there was any updates on this issue. I noticed it was still marked as open in the PITS system and I haven't seen any mention in the Version Notes for the Q1 2010 SP2 release.
0
Tina Stancheva
Telerik team
answered on 14 Jun 2010, 10:20 AM
Hello Francisco Amador,

We are currently working on the issue and it should be ready for the next major release due in a month.

I hope this time frame is acceptable for you.

Kind regards,
Tina Stancheva
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
Francisco Amador
Top achievements
Rank 1
answered on 14 Jul 2010, 05:55 PM
I'm so sorry for bothering you again with this, but I really need the capability to see which was the event initiator in the PreviewChecked/PreviewUnchecked events (and Checked/Unchecked to be consistent I guess). I know that the Q2 release just came out, but I hope that by bringing it up again it might raise the priority level for this.
0
Miroslav
Telerik team
answered on 19 Jul 2010, 02:01 PM
Hello Francisco Amador,

I understand that this is important for you.

We will add a way to distinguish the initiator of the event, it most probably will be a property of the EventArgs.

I expect that the change will be available with this week's internal build. I will follow up here once we have added this.

Regards,
Miroslav
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
Rachel Martin
Top achievements
Rank 1
Answers by
Rachel Martin
Top achievements
Rank 1
Meliton Tienda
Top achievements
Rank 1
Valentin.Stoychev
Telerik team
Miroslav
Telerik team
Francisco Amador
Top achievements
Rank 1
Miro Miroslavov
Telerik team
Tina Stancheva
Telerik team
Share this question
or