How does one style a specific row or rows in a WPF Datagrid during runtime? Each change depends on some values of the shown data?
How does one style a specific row or rows in a WPF Datagrid during
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I can’t tell from your question whether you are adding columns to your grid at run time, but either way you can add a CellStyle to the grid at design time that handles your specific styling needs using DataTriggers.
For instance, the following would make all rows red where the Name property = “Billy Bob”:
If you are adding columns programmatically at run time and you want to apply a certain style to them, you can still define those styles at design time in your xaml.
Then when you are adding the columns you can apply that style to them:
Update
If you have a list of songs and you want to change the row color of every song that has an artist whose name starts with an “a”, then you could use an IValueConverter.
The following converter would do the trick:
Then you could use the converter in your xaml like so:
Notice how I am passing an “a” into the converter as the parameter. You can pass in whatever letter you want, and the rows that have artists that start with that letter will have their background color set to red.
Update 2
If you want to pass in a variable of some sort to the converter, you can use MultiBinding.
The Converter would look like this:
The first parameter passed in is the artist name, the second is the letter.
And you would use it in your grid like this:
In this example, the first letter is coming from the “Text” property of a control called “FirstLetter”. You can change that binding to whatever you want.