3

我有以下课程:

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 属性绑定。我该怎么做呢?提前致谢!

4

1 回答 1

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

方法略有不同,但只是对其进行了测试,它确实有效。:)

于 2010-09-10T19:50:59.887 回答