编程语言
首页 > 编程语言> > c# – 绑定telerik整行的背景(颜色):GridViewDataControl WPF控件

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

作者:互联网

我有以下课程:

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颜色.我该怎么做呢?提前致谢!

解决方法:

另一种方法是使用具有类的绑定的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}"

略有不同的方法,但只是测试它,它肯定有效.

标签:c,wpf,binding,telerik,telerik-grid
来源: https://codeday.me/bug/20190722/1497540.html