Reputation: 765
I got a problem with datagrid and DataGridCheckBoxClumn. First of all im creating struct for datagrid items:
public struct taxRateFromDatabase
{
public int rate { get; set; }
public string mark { get; set; }
public CheckBox c { get; set; }
}
And after that in my class adding columns, bindings etc:
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 });
Problem is that I cant really check/uncheck that checkbox i have just added. I have tried also without checkbox in struct definition (just empty datagridcheckboxcolumn), but that also doesnt work. Im creating it in class which will return datagrid so i cant really acces xaml.
Any sugestions will be appreciated ;)
Upvotes: 0
Views: 1795
Reputation: 2091
I suggest you to use class instead struct (take a look here) and implement INotifyPropertyChanged interface in order to get the binding working.
Something like
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));
}
}
and for example
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;
Upvotes: 1