zoukankan      html  css  js  c++  java
  • DataGrid绑定Dictionary问题

    问题】

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

    【问题重现】

    前台:

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

    后台:

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

    TagModel.cs:

    private bool isSelected = false; 
    public bool IsSelected
    {
                get
                {
                    return isSelected;
                }
    
                set
                {
                    isSelected = value;
                }
     }

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

    【问题分析】

    1.修改前台代码为:

    <DataGridTemplateColumn>                                
    <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding IsSelected}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

    测试无问题。

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

    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

    • CellTemplateCellEditingTemplate 两种情况,即:单元格不可编辑模式和单元格可编辑模式。
    • DataGridCheckBoxColumnCellEditingTemplate继承而来。
    • 对cell进行编辑时,DataGrid的ItemSource 需要一个继承了IList<T>接口的集合,而Dictionary.Values 没有继承该接口,所以会报错。
    • 这也就是为什么使用DataGridCheckBoxColumn会报错而使用CellTemplate不会报错的原因。
    • 当使用List作为数据源,由于List已经继承IList接口,所以DataGridCheckBoxColumnCellEditingTemplate都能够正常使用。

    【总结】

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

    【P.S】

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


    <script type="text/javascript"><!-- google_ad_client = "ca-pub-1944176156128447"; /* cnblogs 首页横幅 */ google_ad_slot = "5419468456"; google_ad_width = 728; google_ad_height = 90; //--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

  • 相关阅读:
    产品经理应该知道的那点事儿(1)
    C语言的前世今生
    “隔代教育的成功之道”新浪教育专家宋少卫做客西单图书大厦
    【9.21更新】跟踪报道TopLanguage关于《深入理解计算机系统(第2版)》一书翻译问题 的讨论
    中国要做物联网技术的强国,而非大国
    关于时钟芯片DS1302的问题
    stm32开发问题集锦
    keil 中使用c++的注意事项
    iar atof 问题
    IAR生成HEX文件
  • 原文地址:https://www.cnblogs.com/sjqq/p/7965837.html
Copyright © 2011-2022 走看看