WPF C1FlexGrid设置样式技巧:(三)设置选择单元格颜色和字体

发布时间:2016/05/21 00:05 发布者:alice

返回博客中心

有了前两篇的基础:WPF C1FlexGrid设置样式技巧:(一)单元格设置背景色WPF C1FlexGrid设置样式技巧:(二)单元格前景色和字体设置,我们想要设置选择单元格的背景色,前景色和字体样式都会非常容易。

除了使用ApplyCellStyles方法,通过CreateCellContent方法,我们也可以实现单元格设置颜色和样式的效果。
本文就在此基础上讨论如何可以设置选择单元格的样式。

CreateCellContent方法设置单元格样式代码参考:

        public override void CreateCellContent(C1FlexGrid grid, Border bdr, CellRange rng)
        {
            base.CreateCellContent(grid, bdr, rng);
            var tb = bdr.Child as TextBlock;

            if (tb != null && rng.Column == 2)
            {
                ContentPresenter cp = (VisualTreeHelper.GetParent(tb) as ContentPresenter);
                System.Windows.Media.RotateTransform rotateTransform = new RotateTransform();
                rotateTransform.Angle = 50;
                tb.LayoutTransform = rotateTransform;
            }
        }

效果:

image

了解了这些知识,我们就可以运用这些知识设置选择的样式。

 

选择的前景色,背景色可以通过SelectionBackground和SelectionForeground直接设置。

其他字体样式依然可以通过继承MyCellFactory的方法实现(CreateCellContent或是ApplyCellStyle)实现。

在方法里添加选择的判断,当选择的时候改变选择的样式,代码参考:

 public override void CreateCellContent(C1FlexGrid grid, Border bdr, CellRange rng)
        {
            base.CreateCellContent(grid, bdr, rng);
            var columnindex = rng.Column;
            var rowindex = rng.Row;
            var tb = bdr.Child as TextBlock;
            bool selected = (columnindex == grid.Selection.Column && rowindex == grid.Selection.Row);
            if (tb != null && selected)
            {
                ContentPresenter cp = (VisualTreeHelper.GetParent(tb) as ContentPresenter);
                System.Windows.Media.RotateTransform rotateTransform = new RotateTransform();
                rotateTransform.Angle = 50;
                tb.LayoutTransform = rotateTransform;
                tb.FontWeight = FontWeights.Bold;
                tb.FontSize = 14;
            }
        }

注意在SelectionChanged事件调用下刷新:

  void flex_SelectionChanged(object sender, CellRangeEventArgs e)
        {
            flex.Invalidate();
        }

效果如图:

image

本文可以下载源代码获取更多的知识:

本文完,如果有兴趣请关注更多资源。

更多资源
C1FlexGrid在线英文产品文档地址:

http://helpcentral.componentone.com/nethelp/C1FlexGridWPFSilv/ComponentOne%20FlexGrid%20for%20WPF%20and%20Silverlight.html

如果你对C1FlexGrid感兴趣,请到我们的官网下载最新版本:/download/?pid=6

如果你有疑问,可以到GCDN论坛获得技术支持:http://gcdn.grapecity.com.cn/showforum-138.html


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网