0

我遇到了 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。

任何建议将不胜感激;)

4

1 回答 1

1

我建议您使用 class 而不是 struct(看看这里)并实现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;
于 2012-09-04T12:18:16.167 回答