【问题标题】:DataGrid CheckBox can't be checked无法选中 DataGrid CheckBox
【发布时间】:2012-09-04 12:06:44
【问题描述】:

我遇到了关于 datagrid 和 DataGridCheckBoxClumn 的问题。首先我为数据网格项目创建结构:

public struct taxRateFromDatabase
{
    public int rate { get; set; }
    public string mark { get; set; }
    public CheckBox c { get; set; }
}

然后在我的班级中添加列、绑定等:

    StackPanel tSp = new StackPanel();
    DataGrid taxRateDataGrid = new DataGrid();
    DataGridTextColumn col0 = new DataGridTextColumn();
    DataGridTextColumn col1 = new DataGridTextColumn();
    DataGridCheckBoxColumn col2 = new DataGridCheckBoxColumn();
    Binding b = new Binding("checkBox");
    b.Mode = BindingMode.TwoWay;

    b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

    taxRateDataGrid.Columns.Add(col0);
    taxRateDataGrid.Columns.Add(col1);
    taxRateDataGrid.Columns.Add(col2);

    col0.Binding = new Binding("rate");
    col1.Binding = new Binding("mark");
    col2.Binding = b;

    CheckBox c = new CheckBox();
    c.Content = "a";


    col0.Header = "Stawka";
    col1.Header = "Oznaczenie";
    col2.Header = "Status";



    taxRateDataGrid.Items.Add(new taxRateFromDatabase { rate = 0, mark = "E", c = c });
    taxRateDataGrid.Items.Add(new taxRateFromDatabase { rate = 1, mark = "G", c = c });

问题是我无法真正选中/取消选中我刚刚添加的复选框。 我也尝试过在结构定义中没有复选框(只是空的datagridcheckboxcolumn),但这也不起作用。我在将返回数据网格的类中创建它,所以我不能真正访问 xaml。

任何建议将不胜感激;)

【问题讨论】:

  • 我看不到您将复选框添加到控件的位置。你在做吗?
  • c = c in taxRateDataGrid (c from class, and = c as checkbox you can see under code)

标签: wpf datagrid checkbox


【解决方案1】:

我建议您使用 class 而不是 struct(看看 here)并实现 INotifyPropertyChanged 接口以使绑定正常工作。

类似

public class TaxRateFromDatabase : INotifyPropertyChanged
        {
            private int _rate;
            public int Rate
            {
                get { return _rate; }
                set { _rate = value; OnPropertyChanged("Rate"); }
            }

            private string _mark;
            public string Mark
            {
                get { return _mark; }
                set { _mark = value; OnPropertyChanged("Mark"); }
            }

            private bool _isChecked;
            public bool IsChecked
            {
                get { return _isChecked; }
                set { _isChecked = value; OnPropertyChanged("IsChecked"); }
            }





            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }

        }

例如

DataGrid taxRateDataGrid = new DataGrid();
        DataGridTextColumn col0 = new DataGridTextColumn();
        DataGridTextColumn col1 = new DataGridTextColumn();
        DataGridCheckBoxColumn col2 = new DataGridCheckBoxColumn();


        taxRateDataGrid.Columns.Add(col0);
        taxRateDataGrid.Columns.Add(col1);
        taxRateDataGrid.Columns.Add(col2);

        col0.Binding = new Binding("Rate");
        col1.Binding = new Binding("Mark");
        col2.Binding = new Binding("IsChecked");

        col0.Header = "Stawka";
        col1.Header = "Oznaczenie";
        col2.Header = "Status";


        List<TaxRateFromDatabase> list = new List<TaxRateFromDatabase>();


        list.Add(new TaxRateFromDatabase { Rate = 1, Mark = "E", IsChecked = true });
        list.Add(new TaxRateFromDatabase { Rate = 23, Mark = "F", IsChecked = false });

        taxRateDataGrid.ItemsSource = list;

【讨论】:

  • 感谢您的回答!我使用了你的代码,就像你写的一样。所以现在我想我不应该创建新的复选框,而是使用 DataGridCheckBoxColumn 吗?像这样:pastebin.com/0qhYWfLp?问因为仍然无法选中/取消选中..
  • @AdamTruszkowski 编辑 col2.Binding = new Binding("isChecked"); to col2.Binding = new Binding("IsChecked");在您链接的第 14 行。
  • @AdamTruszkowski 如果它有效,请考虑将我的答案标记为已接受,谢谢!
  • taxRateDataGrid.Items.Add(new TaxRateFromDatabase { Rate = 1, Mark = "E", IsChecked = true });效果很好(我的意思是在 IsChecked = true 之后)检查了 chkbox,但我无法取消它
猜你喜欢
  • 1970-01-01
  • 2019-02-14
  • 2011-01-01
  • 2019-06-04
  • 2015-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多