Invoke Command when TreeViewItem is Expanded (WPF/MVVM)

GayotFow

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 http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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