我有一个数据网格,项目来源中的一个项目是bool变量。
datagrid将bool转换为复选框。
我想知道如何添加事件复选框,点击数据网格内的复选框?
这是我的代码:
public class FilterMcode
{
private bool cb;
public bool Cb
{
get { return cb; }
set { cb = value; }
}
private string mcode;
public string Mcode
{
get { return mcode; }
set { mcode = value; }
}
private string structureName;
public string StructureName
{
get { return structureName; }
set { structureName = value; }
}
public FilterMcode()
{
cb = true;
structureName = "";
mcode = "";
}
}
public class Main ()
{
private List<FilterMcode> _qf;
update void updateDataGrid()
{
DataGridCommands.ItemsSource = _qf;
DataGridCommands.Items.Refresh();
// calling check box click????
}
}
答案 0 :(得分:1)
使用INotifyPropertyChanged,然后每次选中复选框时,CB属性都会更新。
喜欢那样:
/// <summary>
/// implements the INotifyPropertyChanged (.net 4.5)
/// </summary>
public class BaseObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual void OnPropertyChanged<T>(Expression<Func<T>> raiser)
{
var propName = ((MemberExpression)raiser.Body).Member.Name;
OnPropertyChanged(propName);
}
protected bool Set<T>(ref T field, T value, [CallerMemberName] string name = null)
{
if (!EqualityComparer<T>.Default.Equals(field, value))
{
field = value;
OnPropertyChanged(name);
return true;
}
return false;
}
}
public class FilterMcode:BaseObservableObject
{
private bool cb;
public bool Cb
{
get { return cb; }
set
{
cb = value;
OnPropertyChanged();
}
}
private string mcode;
public string Mcode
{
get { return mcode; }
set
{
mcode = value;
OnPropertyChanged();
}
}
private string structureName;
public string StructureName
{
get { return structureName; }
set
{
structureName = value;
OnPropertyChanged();
}
}
public FilterMcode()
{
cb = true;
structureName = "";
mcode = "";
}
}
然后以下一种方式更改您的主要课程
public class Main
{
private List<FilterMcode> _qf;
public void updateDataGrid()
{
_qf.ForEach(mcode => mcode.PropertyChanged += McodeOnPropertyChanged);
DataGridCommands.ItemsSource = _qf;
DataGridCommands.Items.Refresh();
// calling check box click????
}
private void McodeOnPropertyChanged(object sender, PropertyChangedEventArgs args)
{
if(args.PropertyName != "Cb" ) return;
//Add your logic here
}
}
这是获得所需内容的简单方法。有许多向下:
您必须取消订阅活动。
这不是使用wpf的MVVM方式。
的问候。