无法检查DataGrid CheckBox

时间:2012-09-04 12:06:45

标签: wpf datagrid checkbox

我遇到了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 });

问题是我无法检查/取消选中我刚刚添加的复选框。 我也尝试过在struct definition中没有复选框(只是空datagridcheckboxcolumn),但这也不起作用。我在类中创建它将返回datagrid所以我无法真正访问xaml。

任何sugestions将不胜感激;)

1 个答案:

答案 0 :(得分: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;