回复 10楼安心海叶的帖子
我理解的意思是:如果数据源中有该列数据,则手动添加该列,并将列头设置成TextBlock+CheckBox样式,然后通过CheckBox设置该列数据显示与不显示;然后如果数据源中没有该列数据,则不显示该列?
参考部分代码如下:
- /// <summary>
- /// 创建自定义列
- /// </summary>
- /// <returns></returns>
- private C1.WPF.DataGrid.DataGridTextColumn CreateCustomCol()
- {
- C1.WPF.DataGrid.DataGridTextColumn col = new C1.WPF.DataGrid.DataGridTextColumn();
- Binding binding = new Binding("Test1Actual");
- binding.Mode = BindingMode.TwoWay;
- col.Binding = binding;
- StackPanel sp = new StackPanel();
- sp.Orientation = Orientation.Horizontal;
- TextBlock tb = new TextBlock();
- tb.Text = "TextBlock";
- tb.VerticalAlignment = System.Windows.VerticalAlignment.Center;
- sp.Children.Add(tb);
- CheckBox cb = new CheckBox();
- cb.IsChecked = true;
- cb.VerticalAlignment = System.Windows.VerticalAlignment.Center;
- cb.Click += chk_Click;
- cb.Margin = new Thickness(2, 0, 0, 0);
- sp.Children.Add(cb);
- col.Header = sp;
- return col;
- }
复制代码
- /// <summary>
- /// 复选框事件
- /// </summary>
- private void chk_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- CheckBox chk = sender as CheckBox;
- if (chk.IsChecked.Value)
- {
- m_CustomCol.Binding = new Binding("Test1Actual");
- SVCDataGrid.Refresh(true, true, true, false, false);
- }
- else
- {
- m_CustomCol.Binding = null;
- SVCDataGrid.Refresh(true, true, true, false, false);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
复制代码
然后在自动生成列事件里,对该列替换:
- private void grid_AutoGeneratingColumn(object sender, C1.WPF.DataGrid.DataGridAutoGeneratingColumnEventArgs e)
- {
- if (e.Property.Name == "ParaId")
- {
- _colPara.DisplayMemberPath = "Name";
- _colPara.SelectedValuePath = "Id";
- _colPara.SortMemberPath = "ParaId";
- _colPara.FilterMemberPath = "ParaId";
- _colPara.Header = "Parameter";
- _colPara.Binding = new Binding() { Path = new PropertyPath("ParaId"), Mode = BindingMode.TwoWay };
- _colPara.ItemsSource = SVCParaData.AllPara;
- e.Column = _colPara;
- }
- else if (e.Property.Name == "Test1Actual")
- {
- m_CustomCol = CreateCustomCol();
- e.Column = m_CustomCol;
- }
- }
复制代码 |