我有一个DataGrid,第一列为文本列,第二列为CheckBox列.我想要的是,如果我单击复选框.它应该被检查.
但是,它需要两次单击才能被选中,首次单击该单元格将被选中,第二次单击该复选框将被选中.如何通过单击来选中/取消选中复选框.
我正在使用WPF 4.0.DataGrid中的列是AutoGenerated.
我知道有很多关于该错误的问题(1、2、3、4、5等),但我找不到可以解释此错误原因并适合我的情况的问题。如果我错过了,请告诉我!
首先,我ItemsSource使用自定义类(不是ObservableCollection或任何其他 .NET 内置可观察集合)绑定到我的 DataGrid 。在向您展示它的代码之前,让我解释一下我的想法(我的假设可能是错误的)。
在我看来,要可绑定,集合必须至少实现IEnumerable和INotifyCollectionChanged。IEnumerable 以便视图获取要显示的项目(感谢该GetEnumerator方法)和 INotifyCollectionChanged 以便视图知道集合上的更改。
所以我最终上了这门课:
public class ObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IEnumerable<TValue>, INotifyCollectionChanged
{
#region fields
private IDictionary<TKey, TValue> _innerDictionary;
#endregion
#region properties
public int Count { get { return _innerDictionary.Count; } }
public ICollection<TKey> Keys { get { return _innerDictionary.Keys; } }
public ICollection<TValue> Values { get { return _innerDictionary.Values; } } …Run Code Online (Sandbox Code Playgroud)