Saturday, July 23, 2011

How To Use DataTriggers In Silverlight DataGrid

In this short post I will show you how you can use DataTriggers in datagrid control DataGridTemplateColumn. This was one of the questions when reading one of the forum to change persform some sort of functionality depending on the value of the property. Out of the example code can be seen in the Image 1 here you can see that I have used the DataTrigger for the Subscribed which is nullable Boolean type, mean It hold true , false and null value. Here you can see that I have set verified for true value and also red color and bold font weight, Not varified for the false value and the default value for the textblock is No Information which will handle case for the null value for Subscribed.

Image 1
The xaml code for the data grid control is listed in List 1. Here you can see that I have used DataGridTemplateColumn for the Subscribed column. Then in the CellTemplate I have used DataTemplate and in the DataTemplate I have used stackpanel control and Inside the stackpanel control is my TextBlock control which is used to display value for the Subscribed. Here you can see that after the TextBlock control I have used the triggers. I have used System.Windows.Interactivity dll with alia i which you can see in the namespace area at top. After the Trigger tag I have used the DataTrigger which you can find in the Microsoft.Expression.Interactions dll (I have set alias of ei for the Microsoft.Expression.Interactions dll which is also added in the namespace area at top ).



List 1
I have check for only two values true and false for the IsSubscribed property. In case of true I have set the text Varified, also font weight to bold and font color to red, In case of false I have just set the text property of the textBlock control. In my post you can learn how to change the ChangePropertyAction value using Expression Blend. By using this technique you can also use DataTrigger to disable control , set  visibility of the control etc.You can download the source code from here.

All and any comments / bugs / suggestions are welcomed!

1 comment:

nico008 said...

Hi there!

Great articles here about silverlight datagrid and other things.

Unlucky I still can't get what I want... Can you help me please?

I have a datagrid.
I want it to be row editing.
So when I change a cell content, the IsModified property is set to true and Save/Cancel buttons are displayed.
Now, when a row has IsModified set to true, I want all other rows to have IsEnabled==false

I tryied




but rows never get disabled