简体   繁体   English

根据具体对象的Color属性设置WPF DataGridRow背景颜色

[英]Set WPF DataGridRow Background Color Based on a Color property of a concrete object

I've seen this question asked several times, and it seems like in every case the color is being set in the xaml. 我已经多次问过这个问题,似乎在每种情况下都在xaml中设置颜色。 I already have the colors mapped the way I want in my object. 我已经在对象中以所需的方式映射了颜色。 Please see code: 请查看代码:

public class Alert
{
     public Color BackgroundColor { get; set; }
     public DateTime Expires { get; set; }
     public string Event { get; set; }
     public string AreaDescription { get; set; }
}

I then have a list of Alerts bound to the datagrid. 然后,我有一个绑定到数据网格的警报列表。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();


        this.Alerts.Columns.Add(new DataGridTextColumn()
        {
            Header = "Expires",
            Binding = new Binding("Expires")
        });

        this.Alerts.Columns.Add(new DataGridTextColumn()
        {
            Header = "Event",
            Binding = new Binding("Event")
        });

        this.Alerts.Columns.Add(new DataGridTextColumn()
        {
            Header = "Area Description",
            Binding = new Binding("AreaDescription")
        });

        this.Alerts.ItemsSource = new FeatureCollection().GetFeatures().GetAlerts();
    }
}

My xaml: 我的xaml:

    <Grid>
    <DataGrid x:Name="Alerts" AutoGenerateColumns="False">
        <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
                <Setter Property="Background" Value="{Binding BackgroundColor}"/>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>
</Grid>

The rowstyle setter above didn't work. 上面的行样式设置器无效。 I also tried using data triggers to no avail. 我也尝试使用数据触发器无济于事。

What should happen is the row should get its color from the BackgroundColor property inside the Alert class. 应该发生的是,该行应从Alert类中的BackgroundColor属性获得其颜色。 The background color is set within those chained methods on this line "new FeatureCollection().GetFeatures().GetAlerts();" 在此行的那些链接方法内设置背景色“ new FeatureCollection()。GetFeatures()。GetAlerts();”。 That code is not listed here, just know the color is already set, like BackgroundColor = Color.Yellow; 该代码未在此处列出,只是知道已经设置了颜色,例如BackgroundColor = Color.Yellow;。

Any help would be appreciated. 任何帮助,将不胜感激。 I know this has sort of been asked before, but those answers did not work for me. 我知道以前曾有人问过这个问题,但这些回答对我没有用。 I must be missing something. 我肯定错过了什么。

Your problem come from the fact that BackGroundcolor is not a Color but a brush. 您的问题来自于BackGroundcolor不是Color而是画笔的事实。 So this would work: 所以这可以工作:

public class Alert
{
    public SolidColorBrush BackgroundColor { get; set; }
    public DateTime Expires { get; set; }
    public string Event { get; set; }
    public string AreaDescription { get; set; }
}

and maybe something like this: 也许是这样的:

alerts.Add(new Alert() { BackgroundColor = new SolidColorBrush(Colors.Aqua)});
alerts.Add(new Alert() { BackgroundColor = new SolidColorBrush(Colors.Black) });
alerts.Add(new Alert() { BackgroundColor = new SolidColorBrush(Colors.Blue) });
alerts.Add(new Alert() { BackgroundColor = new SolidColorBrush(Colors.Yellow) });

If you want something even more fancy you could use the Brush type: 如果您想要更精美的东西,可以使用“ Brush类型:

public Brush BackgroundColor { get; set; }

and

alerts.Add(new Alert() { BackgroundColor = new LinearGradientBrush(Colors.Black, Colors.Red, 30) });
alerts.Add(new Alert() { BackgroundColor = new SolidColorBrush(Colors.Black) });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM