2012-09-04 56 views
0

我遇到了datagrid和DataGridCheckBoxClumn的问题。首先对数据网格项目的所有IM创造结构的:DataGrid复选框无法选中

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),但也没有工作。我在类中创建它将返回datagrid,所以我不能真正访问xaml。

任何sugestions可以理解的;)

+0

我不能看到你添加的复选框控制。你在做什么? –

+0

c = c in taxRateDataGrid(c来自class,and = c作为复选框,您可以在代码中看到) – user13657

回答

1

我建议你使用类而不是结构(看看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; 
+0

感谢您的回答! 我用你的代码,就像你写的。所以,现在我想我不应该创建新的复选框,而只是使用DataGridCheckBoxColumn? 那样:http://pastebin.com/0qhYWfLp? 请求becouse仍然不能检查/取消选中.. – user13657

+0

@AdamTruszkowski编辑col2.Binding = new Binding(“isChecked”); to col2.Binding = new Binding(“IsChecked”);在你的链接的第14行。 – michele

+0

@AdamTruszkowski如果它正在工作,请考虑将我的答案标记为已接受,谢谢! – michele