Click here to Skip to main content
12,548,496 members (54,709 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked

WPF TreeView SelectedItemChanged called twice

, 9 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to avoid WPF TreeView SelectedItemChanged being called twice

Very often, we need to execute some code in SelectedItemChanged depending on the selected TreeViewItem. But SelectedItemChanged is called twice. This is due to stealing focus from the main window, which is screwing something up.

What we have to do to avoid this is simply delay the call to our code, i.e., MyFunction() which we need to execute in SelectedItemChanged. Here's a workaround which delays the call to open the new window until the item selection code finishes up:

private delegate void NoArgDelegate();
void Window1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        (NoArgDelegate)delegate { MyFunction(); });


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kanwal Shehzad
Software Developer (Senior) IQ Networks
Pakistan Pakistan
I am Computer Science graduate and working in Microsoft technologies (VB.Net/C#, ASP.Net, WPF, WCF, SQL Server 2005/2008, Web Services).

You may also be interested in...

Comments and Discussions

GeneralYes, it's an anonymous method. Assuming "MyFunction" doesn'... Pin
Richard Deeming14-Jun-11 7:14
memberRichard Deeming14-Jun-11 7:14 
GeneralI think this is an anonymous method, but I'm not entirely su... Pin
KenBonny13-Jun-11 20:04
memberKenBonny13-Jun-11 20:04 
GeneralTruly speaking I do not know the exact name but this syntax ... Pin
Kanwal Shehzad12-Jun-11 1:37
memberKanwal Shehzad12-Jun-11 1:37 
Generalwhat does this means here...there is no "()" after "delegate... Pin
Pritesh Aryan10-Jun-11 23:49
memberPritesh Aryan10-Jun-11 23:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 9 Jun 2011
Article Copyright 2011 by Kanwal Shehzad
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid