Binding Background (color) for the entire row of a telerik:GridViewDataControl WPF control

10,883

Another method is to use a RowStyle that has binding from your class. To avoid having to use any converter or even an event, change your ErrorMessage code to something like this:

public SolidColorBrush background
{
    get
    {        
        switch (this.severity)
        {
            case Severity.Error: return new SolidColorBrush(Colors.Red);                   
            case Severity.Warning: return new SolidColorBrush(Colors.Yellow);
            default: throw new Exception("severity out of bounds");
        }
    }
}

And then add this resource:

        <Style x:Key="xGridViewRowStyle"
               TargetType="telerik:GridViewRow">
            <Setter Property="Background"
                    Value="{Binding background}" />
        </Style>

And on RadGridView:

RowStyle="{StaticResource xGridViewRowStyle}"

Slightly different approach, but just tested it and it definitely works. :)

Share:
10,883
Stephen Swensen
Author by

Stephen Swensen

I'm a software engineer who is particular fond of F# and .NET, but I consider myself a polyglot with a deep and abiding interest in programming language design and compiler construction. Professionally, I've worked across the full stack, but am more focused backend development these days (both Product and Platform focused work). I created Unquote, a popular test assertion library for F# which allows you to write assertions as quoted expressions and get step-by-step failure messages for free. You can check out some of my other work on GitHub. Once upon I time, I worked through the first 50 Project Euler problem to help me learn F#. I also have some old contributions to The Code Project that demonstrate some of my early interest in functional programming and library design. Alas, I am most proud of my role as a father and a husband. And in my free time I enjoy composing and playing music with my friends and family. I've recently started uploading some of my recordings to Bandcamp if you're so inclined!

Updated on June 04, 2022

Comments

  • Stephen Swensen
    Stephen Swensen almost 2 years

    I have the following class:

    public class ErrorMessage
    {
        public enum Severity { Error, Warning}
    
        public ErrorMessage(Severity severity, string description) 
        {
            this.severity = severity;
            this.description = description;
        }
        public Severity severity { get; set; }
        public string description { get; set; }
        public string background
        {
            get
            {
                switch (this.severity)
                {
                    case Severity.Error: return "Red";
                    case Severity.Warning: return "Yellow";
                    default: throw new Exception("severity out of bounds");
                }
            }
        }
    }
    

    And I am binding a List of ErrorMessage to a telerik GridViewDataControl WPF control:

    <telerik:GridViewDataControl Margin="0" telerik:StyleManager.Theme="Office_Silver" Name="errorsGridView" AutoGenerateColumns="False" CanUserSortColumns="False" IsFilteringAllowed="False" ShowGroupPanel="False">
        <telerik:GridViewDataControl.Columns>
            <telerik:GridViewDataColumn IsReadOnly="True" UniqueName="{x:Null}" Header="Severity" DataMemberBinding="{Binding severity}" />
            <telerik:GridViewDataColumn IsReadOnly="True" UniqueName="{x:Null}" Header="Description" DataMemberBinding="{Binding description}" />
        </telerik:GridViewDataControl.Columns>
    </telerik:GridViewDataControl>
    

    I would like the entire Background color of each row to be bound to by the ErrorMessage.background property. How do I do this? Thanks in advance!