Home > C#, WPF > WPF TreeView SelectedItemChanged event firing multiple times

WPF TreeView SelectedItemChanged event firing multiple times

I had an issue today with a WPF TreeView that was very annoying and wasted half of my day with it so it deserved a blog post about it to hopefully save someone else some trouble. 

Basic Summary:

I have a parent item and multiple children in a tree.  I selected one of the child items and it would revert back and select the parent.

Now keep in mind this is a modified Tree View to look like a data grid and there is a lot of processing that goes on behind the scenes in the SelectedItemChanged event. This only occurred when I changed the item in the tree with a mouse click.  If I used the up and down arrows or did it programmatically everything was fine. 

 

Research:

After a couple of hours of digging I found a topic in the MSDN Forums here that talked about how the processing is done internally.  This got me to thinking that it has to be a timing issue and the processing was not completed in time.  The UI Layer was calling a focus method on the tree control which I believe occurred while the Parent item still had a focus set for sub processing behind the scenes.  Since this was the case the SelectedItemChanged event was being fired a second time to set the IsSelected property back to the parent object.   I have not confirmed this but this led me to use the dispatcher to try and resolve. 

Solution:

Using the dispatcher object I was able to run the code inside the selected item changed event and prevent this from occurring.  After wrapping the code inside the BeginInvoke()method the event was only fired once. 

Note the DispatcherPriority.Send parameter is the highest priority you can set for the dispatcher.   

TreeView tv = e.OriginalSource as TreeView;
 e.Handled = true;

 this.Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(
     delegate
     {
            // Do processing here
        
        }));

About these ads
  1. Johannes
    May 25, 2011 at 3:24 am | #1

    Thanks for sharing this solution! Couldnt find anything else around the web..
    My problem was exactly the same and this workaround works fine for me!
    J

  2. Moustafa Deif
    July 27, 2011 at 2:48 am | #2

    Hi Tracy,

    May you describe more details about where to add this code?

    Regards,
    Moustafa Deif

  3. Moustafa Deif
    July 27, 2011 at 2:53 am | #3

    Forgot to tell you that I have the same problem in Silverlight but I have no e.Handled in selected item changed event in Silverlight.

  4. tsells
    July 27, 2011 at 7:40 am | #4

    What control is this for in Silverlight?

  5. September 6, 2011 at 3:36 am | #5

    Thank you very much for this article! It finally helped me after full day of messing with this cycling ItemSelectedChanged event.

  6. Abdelrahman Tawfeek
    September 25, 2012 at 3:59 am | #6

    Thanks a lot sir, you have no idea how much this post helped us.

  1. No trackbacks yet.
You must be logged in to post a comment.
Follow

Get every new post delivered to your Inbox.

Join 166 other followers

%d bloggers like this: