绑定telerik整行的背景(颜色):GridViewDataControl WPF控件

时间:2010-09-10 19:24:05

标签: c# wpf telerik binding telerik-grid

我有以下课程:

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");
            }
        }
    }
}

我正在将一个ErrorMessage列表绑定到telerik GridViewDataControl WPF控件:

<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>

我希望ErrorMessage.background属性绑定每行的整个Background颜色。我该怎么做呢?提前谢谢!

1 个答案:

答案 0 :(得分:10)

另一种方法是使用从您的类绑定的RowStyle。为避免使用任何转换器甚至事件,请将ErrorMessage代码更改为以下内容:

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");
        }
    }
}

然后添加此资源:

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

在RadGridView上:

RowStyle="{StaticResource xGridViewRowStyle}"

稍微不同的方法,但只是测试它,它肯定有效。 :)