WPF中的实现类似Excel的动态条件格式

  条件格式是Excel一个非常常见的功能,所谓动态条件格式,也就是根据数据库的内容,动态的为每个单元格设置格式样式而已。本文主要讨论如何在WPF的网格应用程序中开发实现这一功能。ComponentOne Studio for WPF中的网格控件C1FlexGrid有一个叫CellFactory的类,CellFactory类允许在单元格中自定义网格,接下来就主要用到这个类来实现动态条件格式的效果。

ComponentOne如何实现WPF中的动态条件格式

首先,创建一个继承于CellFactory类的类。

public class CustomCellFactory : CellFactory
{
  
}

然后用CellFactory类来覆盖CreateCellContent()方法,用条件来设置单元式的边框元素的背景。

public override void CreateCellContent(C1FlexGrid grid, Border bdr, CellRange rng)
{
   base.CreateCellContent(grid, bdr, rng);
   //format cells in second column
   if (rng.Column == 2)
   {
      if (grid[rng.Row, rng.Column].ToString() == "Japan")
      {
         bdr.Background = new SolidColorBrush(Colors.LimeGreen);
      }
      else if (grid[rng.Row, rng.Column].ToString() == "India")
      {
         bdr.Background = new SolidColorBrush(Colors.MediumVioletRed);
      }
      else if (grid[rng.Row, rng.Column].ToString() == "United States")
      {
         bdr.Background = new SolidColorBrush(Colors.Yellow);
      }
      else if (grid[rng.Row, rng.Column].ToString() == "United Kingdom")
      {
         bdr.Background = new SolidColorBrush(Colors.Gold);
      }
   }
}

然后动态条件格式就完成了,下面这个GIF就是其动态效果:

ComponentOne如何实现WPF中的动态条件格式

转载于:https://www.cnblogs.com/uncleshu/p/3213708.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值