Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to check data grid check box ?if it is true it should be handle by user defined function? please do the needful to me.
Posted
Comments
Sandeep Mewara 21-Jan-13 11:39am    
if it is true it should be handle by user defined function
Elaborate.

1 solution

The standard way to do this with WPF is to use a pattern called MVVM (Google this pattern, you'll find lots of examples there on how this all hangs together). Now, let's assume that you have a class called MyData that looks like this:
C#
public class MyData : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;
  private bool checkedItem;

  public bool CheckedItem
  {
    get { return checkedItem; }
    set
    {
      if (checkedItem == value) return;
      checkedItem = value;
      OnChanged("CheckedItem");
    }
  }

  private void OnChanged(string property)
  {
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
      handler(this, new PropertyChangedEventArgs(property));
    }
  }
}
Now, because you have a data grid, you are obviously wanting to display many rows, so you create another class that manages the data:
public class ClassToBindTo
{
  public ClassToBindTo()
  {
    MyDataCollection = new ObservableCollection<MyData>();
  }

  public ObservableCollection<MyData> MyDataCollection{ get; private set; }
  public void PopulateData()
  {
    AddDummyData(true);
    AddDummyData(true);
    AddDummyData(false);
    AddDummyData(true);
  }

  private void AddDummyData(bool checkItem)
  {
    MyDataCollection.Add(new MyData { CheckedItem = checkItem });
  }
}
Finally, you hook your data grid up to use MyDataCollection as your ItemsSource (your DataContext would be ClassToBindTo in this example), and the check box would use this as the binding "Checked={Binding CheckedItem}". And that's it, your check box binds to the boolean value, and the boolean value gets checked/unchecked when the checkbox is clicked.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900