【问题标题】:Set WPF DataGridRow Background Color Based on a Color property of a concrete object根据具体对象的 Color 属性设置 WPF DataGridRow 背景颜色
【发布时间】:2019-01-01 20:22:09
【问题描述】:

我已经多次看到这个问题被问到,而且似乎在每种情况下都在 xaml.xml 中设置了颜色。我已经按照我想要的方式在我的对象中映射了颜色。请看代码:

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

然后我有一个绑定到数据网格的警报列表。

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

我的xml:

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

上面的行样式设置器不起作用。我也尝试使用数据触发器无济于事。

应该发生的是,该行应该从 Alert 类中的 BackgroundColor 属性中获取其颜色。背景颜色在“new FeatureCollection().GetFeatures().GetAlerts();”这一行的那些链式方法中设置那个代码这里就不列出了,只知道颜色已经设置好了,比如BackgroundColor = Color.Yellow;

任何帮助将不胜感激。我知道以前有人问过这个问题,但是这些答案对我不起作用。我肯定错过了什么。

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    您的问题来自BackGroundcolor 不是Color 而是刷子这一事实。 所以这会起作用:

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

    也许是这样的:

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

    如果你想要更花哨的东西,你可以使用Brush 类型:

    public Brush BackgroundColor { get; set; }
    

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

    【讨论】:

    • 谢谢@Bob。我回家后会检查一下,然后告诉你它是如何工作的!
    • 我更改了属性,但现在我需要正确的 xaml 语法。到目前为止,背景颜色在我的数据网格中没有做任何事情。
    • 实际上我复制/粘贴了您的 XAML 代码,它对我有用。问题在于绑定逻辑。如果在运行时添加Alert 对象而不再次调用GetFeatures().GetAlerts(),则不会发生任何事情。
    猜你喜欢
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 2023-03-17
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    相关资源
    最近更新 更多