Thursday, May 03, 2012 4:33 AM
I am working on silverlight for the first time. I am using MVVM pattern. I just want to set row back ground color of silverlight datagrid based on the values binded to datagrid. For example assume there is a date of birth column in datagrid. Now the row background should be set based on the month. If DOB is in Jan,row background should be green(say), similarly different colors for different months. I tried binding Row Background property of datagrid, but it is not working.
Thursday, May 03, 2012 2:19 PM
e.Row.Background = new SolidColorBrush(Colors.yourColor);
refer below links:
Friday, May 04, 2012 4:22 AM
I had to do the exact same thing a few weeks ago. It's easy to do in code behind as Jessie has shown but if your trying to do it MVVM its more invloved. Also the codebehind way only works when you reload the datagrid, which I think is impossible from the viewmodel. So you will be able to set the background color on load but then if you have some business logic that you want to happen first then it becomes mroe difficult.
Anyway here's the link to my post were I did the same thing but in MVVM http://social.msdn.microsoft.com/Forums/en-US/silverlightmvvm/thread/0d54bdd9-66ef-45d8-bc81-ba619d309fdf#469fe4b1-7f94-48e8-980a-9e0048612bc6
Basically I created a seperate view model for each of the cells which was responsible for figuring out if it should be highlighted then I bound the grid background color to the view model. Unfortunatly with this approach you need to use TemplateColumns for everything.