C1DataGrid for WPF使用技巧六:改变CheckBox列的外观

发布时间:2015/09/06 00:09 发布者:Alice

返回博客中心

改变CheckBox列外观

在上一篇中,我们讲述了DataTime列的设计C1DataGrid for WPF使用技巧五:绑定TimeSpan属性。本章就接着介绍有关CheckBox列。

在C1DataGrid中,如果包含一个DataGridCheckBoxColumn,你就会发现在Column的单元格中的CheckBox是灰色的,因此它不能编辑。

比如如下单元格是不可用的:

 

 

这种不可用的状态是设计,是因为内部的ReadOnlyCheckBox类。现在如果想要改变这种样式,变成可编辑的状态。

那么本文,就将不可编辑的checkbox列改变状态。

最基本的知识就是使用LoadedCellPresenter事件,然后设置一个CheckBox。

 

代码片段如下:

void c1DataGrid1_LoadedCellPresenter(object sender, C1.Silverlight.DataGrid.DataGridCellEventArgs e)
{
     if (e.Cell.Column.GetType() == typeof(C1.Silverlight.DataGrid.DataGridCheckBoxColumn))
     {
        CheckBox newCheckBox = new CheckBox();
        System.Windows.Data.Binding bnd = new System.Windows.Data.Binding();
        bnd.Source = e.Cell;
        bnd.Path = new PropertyPath("Value");
        bnd.Mode = System.Windows.Data.BindingMode.TwoWay;
        newCheckBox.SetBinding(CheckBox.IsCheckedProperty, bnd);
        e.Cell.Presenter.Content = newCheckBox;
        e.Cell.Presenter.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
        e.Cell.Presenter.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
     }
}

 

因此改变后的样式,就会如下图所示:

 

本文的参考示例如下:

DataGrid_StylingCheckBox_C.zip (32.38 kb)

 

C1DataGrid的在线英文产品文档地址:

http://helpcentral.componentone.com/nethelp/C1datagridWPF/

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

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

 

C1DataGrid for WPF使用技巧》系列文章

(一)在表头展示合计信息

(二)简单数据绑定

(三)C1DataGrid的条件话数据模板


关于葡萄城

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

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