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

发布于 2024-09-18 19:52:55 字数 1463 浏览 1 评论 0原文

我有以下类:

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⒈起吃苦の倖褔 2024-09-25 19:52:56

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

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

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. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文