Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF
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 21-Jan-13 2:26am
Comments
Sandeep Mewara at 21-Jan-13 11:39am
   
if it is true it should be handle by user defined function
Elaborate.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 364
1 Nirav Prabtani 268
2 _Amy 185
3 CHill60 170
4 Sergey Alexandrovich Kryukov 166
0 OriginalGriff 8,004
1 Sergey Alexandrovich Kryukov 7,017
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100