我已经多次问过这个问题,似乎在每种情况下都在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;
任何帮助将不胜感激。我知道以前曾有人问过这个问题,但这些回答对我没有用。我肯定错过了什么。
答案 0 :(得分:0)
您的问题来自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) });