开发者

How to set each row in datagrid with a different color

I have a log window wit开发者_StackOverflow社区h 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜