如何用不同的颜色设置数据网格中的每一行 [英] How to set each row in datagrid with a different color
问题描述
我有一个带有数据网格的日志窗口,每个条目都写入数据网格中的新行。
我要根据行的严重性来设置行颜色日志消息(例如致命异常=红色,信息=绿色)。
我该如何使用绑定或动态资源呢?
预先感谢!
假设这不适用于Silverlight,则可以使用DataTriggers。 p>
假设严重程度
从您的物品中暴露为财产...
< Style TargetType = {x:Type工具箱:DataGridRow}>
< Style.Triggers>
< DataTrigger Binding = {绑定严重性} Value =高>
< Setter Property = Background Value = Red />
< / DataTrigger>
< DataTrigger Binding = {Binding Severity} Value = Medium>
< Setter Property = Background Value = Yellow />
< / DataTrigger>
< DataTrigger Binding = {绑定严重性} Value =低>
< Setter Property = Background Value = Green />
< / DataTrigger>
< /Style.Triggers>
< / Style>
对于Silverlight,您应该使用转换器...
< Style TargetType = {x:Type工具箱:DataGridRow}>
< Setter Property = Background
Value = {Binding Path = Severity,
Converter = {StaticResource StatusToColorConveter}} />
< / Style>
在StatusToColorConveter.Convert()方法中...
switch(value.toString())
{
case High:返回Colors.Red;
case Medium:返回Colors.Yellow;
case Low:返回Colors.Green;
}
让我知道是否有帮助。
I have a log window with a datagrid, each entry write to new row in the datagrid.
I want to set the row color accordingly to the severity of the log message (e.g. fatal exception = Red , Information = Green).
How do i can do it with binding or dynamic resource?
Thanks in advance!
Assuming that this is not for Silverlight, you can use DataTriggers.
Assume Severity
is exposed as a property from your items...
<Style TargetType="{x:Type toolkit:DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding Severity}" Value="High">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding Severity}" Value="Medium">
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
<DataTrigger Binding="{Binding Severity}" Value="Low">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
For silverlight you should use converters...
<Style TargetType="{x:Type toolkit:DataGridRow}">
<Setter Property="Background"
Value="{Binding Path=Severity,
Converter={StaticResource StatusToColorConveter}}"/>
</Style>
In StatusToColorConveter.Convert() method ...
switch(value.toString())
{
case "High": return Colors.Red;
case "Medium": return Colors.Yellow;
case "Low": return Colors.Green;
}
Let me know if this helps.
这篇关于如何用不同的颜色设置数据网格中的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!