[英]Set different background color in datagridrow bound based on datatablerow state
[英]Set WPF DataGridRow Background Color Based on a Color property of a concrete object
我已经多次问过这个问题,似乎在每种情况下都在xaml中设置颜色。 我已经在对象中以所需的方式映射了颜色。 请查看代码:
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();
}
}
我的xaml:
<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;。
任何帮助,将不胜感激。 我知道以前曾有人问过这个问题,但这些回答对我没有用。 我肯定错过了什么。
您的问题来自于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) });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.