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!