在C1DataGrid for Silverlight中,如果包含DataGridCheckBoxColumn类型的列,你会发现该列中单元格处于非编辑状态时,CheckBox都被显示成灰色。



本文也将主要介绍如何改变CheckBox列处于非编辑状态时的显示样式,基本思路是在LoadedCellPresenter事件中将C1.Silverlight.DataGrid.ReadOnlyCheckBox替换为正常的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;
        }
    }
复制代码

运行截图:


源码下载:VS2010 + ComponentOne Studio for Silverlight 2012V2
C#:

DataGrid_StylingCheckBox_C.zip (32.38 K, 下载次数:67)


VB.NET:

DataGrid_StylingCheckBox_VB.zip (32.43 K, 下载次数:34)