Invoke Command when TreeViewItem is Expanded (WPF/MVVM)


To get this working, you can use an attached behaviour, and you’ll see that it’s a clean MVVM strategy.

Create a WPF app and add this Xaml…

Then create a View Model like this…

I use the Relay Command, but you can use the Delegate Command interchangeably. The source for the Relay Command is at

Then create a separate class that looks like this…

Then import the name space of this class into your Xaml…

Resharper will do this for you, or give you an intellesense prompt.

Finally wire up the View Model. Use the quick and dirty method like this…

Then, after the name spaces are resolved and the wiring is correct, it will start to work. Anchor your debugger in the Execute method and observe that you get a RoutedEvent argument. You can parse this to get which Tree view item was expanded.

The key aspect in…

View original post 50 more words


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s