如果检查数据网格复选框如果是,它应该由用户定义的函数处理? [英] how to check data grid check box if it is true it should be handle by user defined function?

查看:67
本文介绍了如果检查数据网格复选框如果是,它应该由用户定义的函数处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查数据网格复选框?如果是,则应该由用户定义的函数处理?请对我这么做。

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.

推荐答案

使用WPF执行此操作的标准方法是使用一种名为MVVM的模式(谷歌这种模式,你会发现很多关于这一切如何挂在一起的例子)。现在,让我们假设您有一个名为MyData的类,如下所示:
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 });
  }
}

最后,您将数据网格挂钩以使用MyDataCollection作为ItemsSource(在此示例中,您的DataContext将是ClassToBindTo),复选框将使用此作为绑定Checked = {Binding CheckedItem}。 就是这样,你的复选框会绑定布尔值,点击复选框时会检查/取消选中布尔值。

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.


这篇关于如果检查数据网格复选框如果是,它应该由用户定义的函数处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆