2009-02-19 127 views
0

我已经用hashtable绑定了一个listview(网格),那么如何从那里获取物品?在使用哈希表之前,我只是做了ListViewA.SelectedItems;,我正在获取源代码。当我用两个foreach循环WPF Listview与Hashtable绑定

铱失败:

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 =模板,我谮它更了解

回答

1

你似乎并不在第二个循环中使用集合。
也许你想

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

或诸如此类。

+0

我不能将模板作为groupCast中的属性,所以我必须在名为Templates的模板类中添加一个Default属性? – 2009-02-19 17:50:46

0
  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的灵感!