【问题标题】:WPF Listview bound with HashtableWPF Listview 与 Hashtable 绑定
【发布时间】:2010-10-08 15:44:30
【问题描述】:

我已经用哈希表绑定了一个列表视图(网格),那么如何从那里获取项目?在使用哈希表之前,我刚刚做了ListViewA.SelectedItems;,我正在获取源代码。

当我使用两个 foreach 循环时,Ir 失败:

dlstTemplates 是我在 WPF 中的 ListView

foreach (var group in dlstTemplates.SelectedItems)
{
  foreach (var Template in group)
  {

  }
}

错误 2 foreach 语句不能 对“对象”类型的变量进行操作 因为“对象”不包含 公共定义 'GetEnumerator' D:\cs_InformeMedico\app\Template.xaml.cs 85 21 Demo.View

我在调试器上发现了这个:

-       dlstPlantillas.SelectedItems    Count = 1   System.Collections.IList {System.Windows.Controls.SelectedItemCollection}
-       [0] {System.Data.Linq.SqlClient.ObjectReaderCompiler.Group<string,Demo.View.Plantilla>} object {System.Data.Linq.SqlClient.ObjectReaderCompiler.Group<string,Demo.View.Plantilla>}
-       Non-Public members      
+       items   Count = 97  System.Collections.Generic.IEnumerable<Demo.View.Plantilla> {System.Collections.Generic.List<Demo.View.Plantilla>}
        key "101010112000"  string
        System.Linq.IGrouping<K,T>.Key  "101010112000"  string
-       Results View    Expanding the Results View will enumerate the IEnumerable   
+       [0] {Demo.View.Plantilla}   Demo.View.Plantilla
+       [1] {Demo.View.Plantilla}   Demo.View.Plantilla
+       [2] {Demo.View.Plantilla}   Demo.View.Plantilla
+       [3] {Demo.View.Plantilla}   Demo.View.Plantilla
+       [4] {Demo.View.Plantilla}   Demo.View.Plantilla
+       [5] {Demo.View.Plantilla}   Demo.View.Plantilla
+       [6] {Demo.View.Plantilla}   Demo.View.Plantilla
+       [7] {Demo.View.Plantilla}   Demo.View.Plantilla
+       [8] {Demo.View.Plantilla}   Demo.View.Plantilla
+       [9] {Demo.View.Plantilla}   Demo.View.Plantilla
+       [10]    {Demo.View.Plantilla}   Demo.View.Plantilla
-       Raw View        
-       [System.Windows.Controls.SelectedItemCollection]    Count = 1   System.Windows.Controls.SelectedItemCollection
+       [0] {System.Data.Linq.SqlClient.ObjectReaderCompiler.Group<string,Demo.View.Plantilla>} object {System.Data.Linq.SqlClient.ObjectReaderCompiler.Group<string,Demo.View.Plantilla>}
+       Raw View        
        IsFixedSize false   bool
        IsReadOnly  false   bool

所以当组是一个

{System.Data.Linq.SqlClient.ObjectReaderCompiler.Group<string,Demo.View.Plantilla>} object type

抱歉,Plantilla = 模板,我将其引述为更容易理解

【问题讨论】:

    标签: wpf listview binding


    【解决方案1】:

    您似乎没有在第二个循环中使用集合。
    也许你想要

    foreach(var group in dlstTemplates.SelectedItems)
    {
        foreach(var Template in groupCast.Templates)
        {
            //do stuff...
        }
    }
    

    或类似的。

    【讨论】:

    • 我无法将模板作为 groupCast 中的属性,所以我必须在名为 Templates 的模板类中添加默认属性?
    • 当我尝试得到错误 1“模板”:成员名称不能与其封闭类型相同 D:\cs_InformeMedico\app\Template.cs 26 26 Demo.View
    • 没有。您试图在第二个循环中枚举单个对象。你使用 foreach 你必须在一个集合上工作。 foreach(var thing in (int)17) 将产生与您遇到的相同的错误。
    • 也许如果您在 dlstTemplates.SelectedItems 中发布您的收藏成员的代码)我们可以解决这个问题
    【解决方案2】:
                foreach (var group in dlstPlantillas.SelectedItems)
                {
                    IGrouping<string, Plantilla> groupCast = group as System.Linq.IGrouping<string, Plantilla>;
                    if (null == groupCast) return;
                    foreach (Plantilla item in groupCast)
                    {
                        template.codigoestudio = item.codigoestudio;
    
                    }
                }
    

    希望有所帮助...感谢 ZombieSheep 的启发!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-15
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多