XAML Power Toys & MoXAML – the one-two knockout punch.

In this post, I’d like to talk about how MoXAML and XAML Power Toys can work together to produce applications quickly and easily. We’re going to look at how to create a working fully databound datagrid in Silverlight, in 1 minute.

Important note:
If you’re running Visual Studio on Vista, please run it as Administrator to follow this tutorial.

So, now that we know what we’re trying to do, let’s start off by creating a Silverlight application in Visual Studio 2008. For this run through, we need to use C# because we are going to use the Notify Property command which works against automatic properties, which are only a feature of C# (unfortunately). For this sample, I’m adding a new ASP.NET web project to host Silverlight, and I’ve set the Project Type to ASP.NET Web Application Project.

Right, now that we’ve created the basic application, we’re going to add a new class to the Silverlight control project which will be the model we’re planning on adding. Normally, we’d create a new DLL for the model, but for the sake of simplicity I’m adding it directly to the control. I’m calling this class Person, and it will consist of 4 properties; Title, Name, Age and Nickname. The class looks like this:

Now, we want to convert the class so that the properties use the PropertyChanged event handler. To do this, I’m going to add the INotifyPropertyChanged interface to the class definition and then implement the interface. (You will need to add using System.ComponentModel; to your class). At this stage, the class looks like:

The problem with the code as it stands is that it won’t properly participate in databinding, because it’s not going to notify bound items of any changes. That’s where the PropertyChanged event comes in. I love automatic properties, but I was so frustrated that they didn’t internally implement this – this was the reason that I wrote the Notify Property command. So, it’s time to fire up MoXAML and rectify the “deficiencies”.

Before you continue, it’s important to make sure that you have the latest versions of XAML Power Toys and MoXAML Power Toys, and that they are running as an add-in in Visual Studio.

Right click on the Name property and click MoXAML Power Toys > Make Notify Property from the menu. This adds the necessary “plumbing” to make this a notify property.

MoXAML menu

MoXAML converted code

Repeat this with the other properties, to add the relevant implementation into each property. Now we’re going to move onto the Silverlight control. At this stage, it is vital that you build your project, and ensure that it compiles successfully because XAML Power Toys uses reflection to work out what is available in your class.

Once you’ve built the project, right click on the class in the solution explorer and choose XAML Power Toys > Create Form, ListView or Data Grid For Class. When the dialog appears, choose Silverlight Data Grid in the Select Object To Create dropdown.

XAML Power Toys form in action.

Now, we can drag and drop the items we want to appear in the grid. I’m going to add them in the order Title, Name, Nickname and Age. For more information on the template items available in the dialog, please see the videos that Karl has most helpfully produced, but for the moment, please ensure that the mode is set to TwoWay for each item.

Items being added to the datagrid.

Once you’ve added the fields you want, click Create. If this is the first time you’ve used XAML Power Toys, you should see an information box with the message: “Your settings file has been created for you. You can configure your settings using the Set Control Defaults command.” Click OK.

You’ll now see an information box that tells you that you can now paste your XAML in.

Once you’ve pasted your grid into the Page.xaml file, you may notice that the DataGrid has lots of wavy underlines. This is because the System.Windows.Controls.Data assembly isn’t referenced in your project, and the namespace hasn’t been added to the UserControl attributes. To fix this, add the reference into your project, and copy the definition xmlns:data=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data” into the UserControl attributes.

We’re going to give our datagrid a name. Let’s call it xamlGrid. To do this, add x:Name=”xamlGrid” to the datagrid. We have done this so that we can hook the grid up with some data. So now, let’s add the Loaded event to our usercontrol. To do this, add Loaded=”UserControl_Loaded” into the control definition (if you type Loaded= into Visual Studio, and press enter when the <New Event Handler> is highlighted, the event handler is added into both the XAML and the codebehind.

Now, open up Page.xaml.cs and take a look at the code. The UserControl_Loaded handler should be present, and hooks up to a RoutedEvent. It’s now time to add some data and bind it in. First of all, let’s add a list of Person objects as a class member. Once this has been done, add in some code to add new Person items to the list and then bind the list to the datagrid ItemsSource. Here’s my code:

If you build and run this project, you’ve got a fully databound grid which displays the items you entered. Change some values, and the properties update as you’d expect. If you want to test this, and I strongly suggest you do because it’s so cool, create a copy of your first grid in the XAML, change it’s name and bind it to the same list. Rerun the project, and change a value in one of the grids – if you’ve done everything right, the other grid will display your updated values.

That’s all there is to getting started with using MoXAML and XAML Power Toys to quickly create a databound Silverlight application. It really is that easy, and I’d like to thank Karl for his hard work in making it so that it is that easy.


You can download the sample here. As always, you’ll need to rename the .doc file to .zip before you can extract it.



6 thoughts on “XAML Power Toys & MoXAML – the one-two knockout punch.

  1. Great tutorial.I was able to follow it and get up and running. Your dependancy macro is a massive time saver. My only feedback is the line “add Loaded=”UserControl_Loaded” into the control definition” may lose some beginners so a better screen shot may help so they know exactly where to put it. Also if we could cut and paste the sample code to save some typing 🙂

  2. peteohanlon

    Good stuff Paul. I’m glad it all worked out for you. MoXAML is designed to complement Karl’s stuff, so expect to see more of this type of stuff.

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