【发布时间】: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;
任何帮助将不胜感激。我知道以前有人问过这个问题,但是这些答案对我不起作用。我肯定错过了什么。
【问题讨论】: