找回密码
 立即注册

QQ登录

只需一步,快速开始

ZenosZeng 讲师达人认证 悬赏达人认证
超级版主   /  发表于:2012-3-9 09:53  /   查看:7114  /  回复:0
在WinForms和WebForms程序中,我们使用表格控件时经常会有这样的需求:如果这一列的单元格时CheckBox类型,我们希望在列头也添加一个CheckBox,通过对列头CheckBox的操作来完成对该列所有CheckBox的选中/不选中操作。下面我们就来看一看在C1FlexGrid for Silverlight中如何实现这一功能。

功能要求:
  • 当对列头中的CheckBox进行选中操作时,这一列中所有的CheckBox将被选中
  • 当对列头中的CheckBox进行不选中操作时,这一列中所有的CheckBox将不被选中
  • 当这一列中所有CheckBox都被选中时,列头中的CheckBox也应该是选中状态
  • 当这一列中所有CheckBox都不被选中时,列头中的CheckBox也应该是不选中状态


实现步骤:
第一步:设置数据源,我们以个人信息作为FlexGrid的数据源,代码如下:
  1. public class Person : INotifyPropertyChanged
  2. {
  3.     private int id;
  4.     private string fname;
  5.     private string lname;
  6.     private bool present;

  7.     public event PropertyChangedEventHandler PropertyChanged;

  8.     public int ID
  9.     {
  10.         get { return id; }
  11.         set
  12.         {
  13.             id = value;
  14.             OnPropertyChanged("ID");
  15.         }
  16.     }


  17.     public string FName
  18.     {
  19.         get { return fname; }
  20.         set
  21.         {
  22.             fname = value;
  23.             OnPropertyChanged("FName");
  24.         }
  25.     }

  26.     public string LName
  27.     {
  28.         get { return lname; }
  29.         set
  30.         {
  31.             lname = value;
  32.             OnPropertyChanged("LName");
  33.         }
  34.     }

  35.     public bool Present
  36.     {
  37.         get { return present; }
  38.         set
  39.         {
  40.             present = value;
  41.             OnPropertyChanged("Present");
  42.         }
  43.     }

  44.     protected void OnPropertyChanged(string name)
  45.     {
  46.         PropertyChangedEventHandler handler = PropertyChanged;
  47.         if (handler != null)
  48.         {
  49.             handler(this, new PropertyChangedEventArgs(name));
  50.         }
  51.     }
  52. }
复制代码
  1. List<Person> datasource = new List<Person>();

  2. #region "Test Data"
  3. datasource.Add(new Person() { ID = 1, FName = "Robert", LName = "Ludlum", Present = true });
  4. datasource.Add(new Person() { ID = 2, FName = "Tom", LName = "Clancy", Present = true });
  5. datasource.Add(new Person() { ID = 3, FName = "Frederick", LName = "Forsyth", Present = false });
  6. datasource.Add(new Person() { ID = 4, FName = "Dan", LName = "Brown", Present = true });
  7. datasource.Add(new Person() { ID = 5, FName = "Paulo", LName = "Coelho", Present = false });
  8. datasource.Add(new Person() { ID = 6, FName = "John", LName = "Grisham", Present = false });
  9. #endregion

  10. this.c1FlexGrid1.ItemsSource = datasource;
复制代码
第二步:设置XAML中FlexGrid各个列的数据绑定信息
  1. <Grid x:Name="LayoutRoot" Background="White">
  2.     <my:C1FlexGrid Name="c1FlexGrid1" AutoGenerateColumns="False">
  3.         <my:C1FlexGrid.Columns>
  4.             <my:Column Header="ID" Binding="{Binding ID, Mode=OneWay}"/>
  5.             <my:Column Header="First Name" Binding="{Binding FName, Mode=TwoWay}"/>
  6.             <my:Column Header="Last Name" Binding="{Binding LName, Mode=TwoWay}"/>
  7.             <my:Column Header="Present" Binding="{Binding Present, Mode=TwoWay}">
  8.             </my:Column>
  9.         </my:C1FlexGrid.Columns>
  10.     </my:C1FlexGrid>
  11. </Grid>
复制代码
第三步:实现选择/不选择的行为
为了在boolean类型的列中添加CheckBox功能,需要实现用户自定义的C1.Silverlight.FlexGrid.CellFactory类,在CheckBox类型的列中,我们需要处理以下操作:
1.  选择/不选择列头中的CheckBox控件
2.  选择/不选择单元格中的CheckBox控件

下面的代码重写了CreateColumnHeaderContent方法,以此来创建列头中的CheckBox控件,并通过CheckBox的Click事件来改变其选中和不选中状态:
  1. //实现列头中的CheckBox功能
  2. public override void CreateColumnHeaderContent(C1.Silverlight.FlexGrid.C1FlexGrid grid, Border bdr, C1.Silverlight.FlexGrid.CellRange range)
  3. {
  4.     base.CreateColumnHeaderContent(grid, bdr, range);
  5.     if (grid.Columns[range.Column].DataType == typeof(Boolean))
  6.     {
  7.         chk = new CheckBox();
  8.         chk.Content = "Present";
  9.         bdr.Child = chk;
  10.         parentgrid = grid;
  11.         parentrange = range;
  12.         // 添加Click事件,处理其选择/不选择状态
  13.         chk.Click += new RoutedEventHandler(chk_Click);
  14.     }
  15. }


  16. void chk_Click(object sender, RoutedEventArgs e)
  17. {
  18.     //如果列头中的CheckBox为选中状态,就设置该列中所有CheckBox为选中状态
  19.     if ((sender as CheckBox).IsChecked == true)
  20.     {
  21.         for (int i = 0; i <= parentgrid.Rows.Count - 1; i++)
  22.         {
  23.             parentgrid[i, parentrange.Column] = true;
  24.         }
  25.     }
  26.     else
  27.     //如果列头中的CheckBox为不选中状态,就设置该列中所有CheckBox为不选中状态
  28.     {
  29.         for (int i = 0; i <= parentgrid.Rows.Count - 1; i++)
  30.         {
  31.             parentgrid[i, parentrange.Column] = false;
  32.         }
  33.     }
  34. }
复制代码
下面,需要处理单元格中所有CheckBox的选择/不选择操作。通过重写CreateCellContent方法可以处理单元格中CheckBox的选中/不选中状态,并依赖于这些状态来修改列头中CheckBox的状态,代码如下:
  1. //实现单元格中的CheckBox功能
  2. public override void CreateCellContent(C1.Silverlight.FlexGrid.C1FlexGrid grid, Border bdr, C1.Silverlight.FlexGrid.CellRange range)
  3. {
  4.     base.CreateCellContent(grid, bdr, range);
  5.     if (bdr.Child.GetType() == typeof(CheckBox))
  6.     {
  7.         CheckBox childCkBox = bdr.Child as CheckBox;               
  8.         childCkBox.Checked += new RoutedEventHandler(childCkBox_Checked);
  9.         childCkBox.Unchecked += new RoutedEventHandler(childCkBox_Unchecked);
  10.     }
  11. }

  12. // 如果该类所有单元格中CheckBox都是选中状态,就将列头中的CheckBox设置为选中状态
  13. void childCkBox_Checked(object sender, RoutedEventArgs e)
  14. {
  15.     bool chkflag = true;   
  16.     for (int i = 0; i < parentgrid.Rows.Count; i++)
  17.     {
  18.         if ((bool)parentgrid[i, parentrange.Column] == false)
  19.         {
  20.             chkflag = false;
  21.             break;
  22.         }
  23.     }

  24.     if (chkflag)
  25.         chk.IsChecked = true;
  26. }

  27. // 如果该类所有单元格中CheckBox都是未选中状态,就将列头中的CheckBox设置为未选中状态
  28. void childCkBox_Unchecked(object sender, RoutedEventArgs e)
  29. {
  30.     chk.IsChecked = false;
  31. }
复制代码


最终效果:


附上源码:VS2010 + ComponentOne for Silverlight 2011V3

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部