If you are using the Infragistics xamDockManager control and using MVVM to compose your views, then you have probably asked yourself the question, “How do I data bind a collection of objects, which represent my Views, from my ViewModel to various areas of the xamDockManager?”. You are asking yourself this question because the xamDockManager doesn’t support this data binding out of the box. The good news is that this is relatively easy to accomplish. You just have to write a little code. As with most solutions, there are always more than one way to solve a problem. There are actually solutions to this specific problem that have already been posted. For example, this post titled “ItemsSource for xamDockManager”, provides an alternative to the solution I am going to show you. So why am I showing you another approach? Well, I prefer something a little more simple and straight forward. You can choose which solution you prefer.
In this post, I will be adding the required MVVM support to the WPF version of the xamDockManager, and we will be using a Behavior to do it. My Behavior is going to target a TabGroupPane as my container of choice. What I mean by “container of choice”, is that I am going to have all my Views data bound and hosted inside of a TabGroupPane. You may choose something different, such as adding support to the xamDockManager directly, or maybe use the DocumentContentHost. What ever floats your boat!
Now, this behavior should support a couple of different scenarios.
- I should be able to data bind a collection of objects, which will represent a view, from a ViewModel
- I should be able to add new objects to this collection and have the View be shown in a new tab in the xamDockManager
- I should be able to remove an existing object from the collection and have the View removed from the xamDockManager
- I should also be able to close a tab and have the object automatically removed from the collection
- I should be able to provide a DataTemplate to define the structure of the object as the View (implicit or explicit)
- I should be able to provide a property path to use as the tab header text
- I should be able to provide a DataTemplate to define the structure of the tab header
That about wraps it up. Wow, that’s a lot of stuff, but nothing a simple Behavior can’t solve for us. I would like to note that I am not concerned with removing objects from one collection and adding them to another collection in my ViewModel when dragging and dropping tabs around the xamDockManager. If you want that, you will have to add that functionality yourself. I am just going to provide you with the behavior, then talk about it.
As you can see we have only a couple of properties. The HeaderMemberPath is used to specify the property path of the underlying object to use as the text for the tab header. The HeaderTemplate property is used to control the structure of the tab header. For example, maybe we want to add images or change other aspects of the tab header. Next, we have the ever popular ItemsSource property. You will use this property to data bind your collection of objects from your ViewModel to the TabGroupPane of the xamDockManager. Notice how I am using IList as the property type. This allows us to add and remove items from the collection. Lastly, we have the ItemTemplate property. This property will allow us to provide a DataTemplate to define the structure of each of the items in the collection. Now keep in mind, the ItemTemplate will only really work if all the objects in your collection are the same. If you have a collection of different object types, then you will not be using the ItemTemplate property. Feel free to add more properties and modify this behavior to fit your specific needs.
So how do you use this behavior? Well let’s start with a ViewModel.
This is a very straight forward ViewModel. It has a single collection and two commands. One command will insert a new Person object into my collection and the other command will remove an instance of a Person object from the collection. Speaking of the Person object, let’s take a look at it.
Notice how I am not implementing INotifyPropertyChanged. This is only because this is meant to be sample code and not meant to replicate a production system. When you do this, make you’re your ViewModels and POCOs implement INotifyPropertyChanged.
Next, let’s define our behavior in our View. Since we are using a Behavior, you need to add a reference to System.Windows.Interactivity to your WPF application and add an namespace in your XAML.
Let’s run the application and see what we have so far.
That’s cool and all, but our objects don’t really looks like views right now, and what’s up with the tab header? Let’s start using some of those properties we created to make this look a litle better. Let’s start with the header. I want to bind the tab header to the FullName property of our Person object. I also want to make some changes to the formatting of the tab header so I am going to create a new DataTemplate for it. I am also going to define an DataTemplate to use as the ItemTemplate for my Person object.
Let’s see what we have with our changes.
Now that’s better! You don’t have to use the ItemTemplate property. If you have a collection of objects, for example ObservableCollection<object>, you can provide an implicit DataTemplate to the various types you are adding to the collection. Maybe you have a DataTemplate for Person, and a different one for Car, and a different one for Pet. You can create a different DataTemplate for each of these types and they will be rendered properly for each corresponding tab.
That’s wraps it up for this post. Keep in mind that this post is mainly to help guide you in implementing MVVM with the xamDockManager control and you will probably have to modify this code to meet your specific needs. Go ahead and download the source code for this post. Feel free contact me on my blog, connect with me on Twitter (@brianlagunas), or leave a comment below for any questions or comments you may have.