DataGrid绑定Dictionary问题

【问题】

在最近的项目中使用DataGrid的DataGridCheckBoxColumn绑定了后台TagModel类的IsSelected字段,数据源是TagModel类型的Dictionary,运行发现Checkbox不能正常勾选,提示错误:此视图不允许“EditItem”。

【问题重现】

前台:

1
<DataGridCheckBoxColumn Binding="{Binding IsSelected}" />

后台:

1
2
3
4
Dictionary<string, TagModel> dicTag = new Dictionary<string, TagModel>();
TagModel tm = new TagModel ();
dicTag.Add("Test",tm);
dgTest.ItemsSource = dicTag.Values;

TagModel.cs:

1
2
3
4
5
6
7
8
9
10
11
12
13
private bool isSelected = false; 
public bool IsSelected
{
get
{
return isSelected;
}

set
{
isSelected = value;
}
}

运行后点击CheckBox就会弹出错误:此视图不允许“EditItem”。

【问题分析】

1.修改前台代码为:

1
2
3
4
5
6
7
<DataGridTemplateColumn>                                
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

测试无问题。

2.不修改前台代码,修改后台代码为

1
2
3
4
5
Dictionary<string, TagModel> dicTag = new Dictionary<string, TagModel>();
TagModel tm = new TagModel ();
dicTag.Add("Test",tm);
List<TagModel> lstTM = new List<TagModel>(dicTag.Values);
dgTest.ItemsSource = lstTM ;

测试也无问题。

分析:1.使用Dictionary的Values作为数据源时,DataGridTemplateColumn可以正常操作而DataGridCheckBoxColumn会报错,猜测这两种Column采用了不同的处理机制。

2.使用List复制Dictionary的Values作为数据源时,DataGridTemplateColumn和DataGridCheckBoxColumn都能正常操作,由于Dictionary的Values没有继承IList接口而List继承了该接口,猜测在使用没有继承IList的变量作为数据源时DataGridCheckBoxColumn和DataGridTemplateColumn的处理机制不同。

【问题解决】

通过MSDN版主Lisa Zhu的解释,最终弄明白问题所在http://social.msdn.microsoft.com/Forums/zh-CN/wpfzhchs/thread/cd25f422-dabc-4d1c-a401-16f9b41c1dfd

  • CellTemplate和**CellEditingTemplate **两种情况,即:单元格不可编辑模式和单元格可编辑模式。

  • DataGridCheckBoxColumnCellEditingTemplate继承而来。

  • 对cell进行编辑时,DataGrid的ItemSource 需要一个继承了IList接口的集合,而Dictionary.Values 没有继承该接口,所以会报错。

  • 这也就是为什么使用DataGridCheckBoxColumn会报错而使用CellTemplate不会报错的原因。

  • 当使用List作为数据源,由于List已经继承IList接口,所以DataGridCheckBoxColumnCellEditingTemplate都能够正常使用。

【总结】

使用Dictionary的Values作为数据源时需要将Dictionary的Values先复制到List中,再将List作为数据源绑定。

【P.S】

如果使用Dictionary的Values作为数据源并使用**CellEditingTemplate **绑定,可以将该列的IsReadOnly设为True,则始终不会使用CellEditingTemplate属性值,就不会抛出错误。但同时也不能操作这一列了。

作者

Wu Rang

发布于

2013-07-04

更新于

2021-12-06

许可协议

评论