WPF使用转换器更改datagrid单元格背景色

编程入门 行业动态 更新时间:2024-10-27 06:26:26
本文介绍了WPF使用转换器更改datagrid单元格背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个WPF数据网格。我需要比较两列类型为datetime的列,并根据比较结果,为当前列和行中的两个单元格设置单元格背景色。我为每个datagrid行执行此操作。为此,我使用了转换器。

I have an WPF datagrid. There are two columns of type datetime which I need to compare and according to the compare result, I set a cell background color for the two cells in the current column and row. I do this for each datagrid row. In order to do this I use a converter.

<my:DataGridTextColumn Binding="{Binding Path=Date1, StringFormat=\{0:dd/MM/yyyy\}}" Header="Date"> <my:DataGridTextColumn.ElementStyle> <Style TargetType="TextBlock"> <Setter Property="Background"> <Setter.Value> <MultiBinding Converter="{StaticResource CellDateColorConverter}"> <Binding Path="Date1"/> <Binding Path="Date2"/> </MultiBinding> </Setter.Value> </Setter> </Style> </my:DataGridTextColumn.ElementStyle> </my:DataGridTextColumn> <my:DataGridTextColumn Binding="{Binding Path=Date2, StringFormat=\{0:dd/MM/yyyy\}}" Header="Date"> <my:DataGridTextColumn.ElementStyle> <Style TargetType="TextBlock"> <Setter Property="Background"> <Setter.Value> <MultiBinding Converter="{StaticResource CellDateColorConverter}"> <Binding Path="Date1"/> <Binding Path="Date2"/> </MultiBinding> </Setter.Value> </Setter> </Style> </my:DataGridTextColumn.ElementStyle> </my:DataGridTextColumn>

转换器:

public class CellDateColorConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (values[0] is DateTime && values[1] is DateTime) { DateTime date1 = (DateTime)values[0]; DateTime date2= (DateTime)values[1]; if (date1.Date > date2.Date) { return Color.Brown; } } return ????? // I need to return the default datagrid cell's background color. How to do this? } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotSupportedException("CellDateColorConverter is a OneWay converter."); } }

这里我有两个问题:

  • 当date1> date2单元格背景色未更新为棕色时。
  • 如果date1 <= date2,默认的datagrid单元格背景色应该返回,我不知道该怎么做。
  • 我还为数据网格。行样式根据某些条件设置整个行的背景颜色。但是在这种情况下,这些条件不能满足,但是上面的列样式(date1.Date> date2.Date)可以满足,因此单元格背景应该用棕色绘制。

    Also I have defined a row style for the datagrid. The row style sets the entire row background color according to some conditions. But in this case, these conditions are not satisfied but the above column style (date1.Date > date2.Date) does, so cell background should be painted with brown.

    利用此帖子,如果满足行样式的条件,并且整个背景设置为例如橙色,如果也满足单元格列样式(此帖子上方)并且需要用棕色绘制,那么哪个占优势?是行样式还是单元格样式?

    Taking advantage of this post, in case conditions for row style are satisfied, and entire background is set for example to orange, if cell column style (above in this post) is also satisfied and need to be painted in brown, then which prevails? the row style or cell style?

    推荐答案

  • 返回画笔: if (date1.Date > date2.Date) { return System.Windows.Media.Brushes.Brown; }

  • 返回 System.Windows.Data。 Binding.DoNothing 。

  • 更多推荐

    WPF使用转换器更改datagrid单元格背景色

    本文发布于:2023-11-05 04:23:56,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1559891.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:转换器   单元格   背景色   WPF   datagrid

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!