2012-08-26 47 views
1

我有一本字典,其中键是一个String和元素是一个列表如何创建GridView控件从字典

我想从与元素元素每个键创建一个组。但我不`吨知道如何使它

<Page.Resources> 
    <!-- 
     Collection of grouped items displayed by this page, bound to a subset 
     of the complete item list because items in groups cannot be virtualized 
    --> 
    <CollectionViewSource 
     x:Name="groupedItemsViewSource" 
     IsSourceGrouped="true" 
     /> 

</Page.Resources> 

<GridView ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}" 
     IsSwipeEnabled="True"> 
      <GridView.ItemTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding name}" 
      Foreground="White" /> 
       </DataTemplate> 
      </GridView.ItemTemplate> 
      <GridView.ItemsPanel> 
       <ItemsPanelTemplate> 
        <StackPanel Orientation="Horizontal" /> 
       </ItemsPanelTemplate> 
      </GridView.ItemsPanel> 
      <GridView.GroupStyle> 
       <GroupStyle> 
        <GroupStyle.HeaderTemplate> 
         <DataTemplate> 
<TextBlock Text="Test 123" Foreground="Gold" /> 
         </DataTemplate> 
        </GroupStyle.HeaderTemplate> 
        <GroupStyle.Panel> 
         <ItemsPanelTemplate> 
          <VariableSizedWrapGrid Orientation="Vertical" /> 
         </ItemsPanelTemplate> 
        </GroupStyle.Panel> 
       </GroupStyle> 
      </GridView.GroupStyle> 
     </GridView> 

在环路我创建字典

groups.Add(this.letters[i], items); 

之后,我有

groupedItemsViewSource.Source = groups; 

但我什么也没得到。我应该如何解决这个问题,以便将组合键和每个列表都作为这个网格中的元素列表?

//编辑

好的我发现使用List>而不是Dictionary是更好的。我现在得到3组标题(因为我列表中有3个列表),但没有任何项目。 IMO这是比更好的办法第一

// EDIT 2 我didn`t看到物品引起的背景和前景是白色的。愚蠢的我:)但现在我有最后一个问题来解决。如何动态设置组标题?如果你想

+0

不要改变你的问题。它使我们在一个移动的目标上开枪。 –

+0

@Erno对不起,我只是尽量给尽可能多的信息,我可以 – Fixus

+0

这不是更多的信息,这只是另一个问题。想象一下,有些人与你添加的最后一个人有同样的问题。他在这个问题上绊倒了什么? –

回答

1

你其实不必创建自定义类,使分组。事实上,你可以使用LINQ来为你做它:

VAR的结果=从行为活动组由act.Project行为变成GRP 排序依据grp.Key选择GRP;

cvsActivities.Source = result;

为了在GridView中显示分组项目,您必须使用CollectionViewSource。只需将组设置为GridView.ItemsSource不起作用。您必须设置GridView.ItemsSource = CollectionViewSource和CollectionViewSource必须指向组。您还可能必须设置CollectionViewSource.IsSourceGrouped = true。

0

字典没有实现所需要INotifyPropertyChanged的或INotifyCollectionChanged你的绑定工作

public class yourclass 
{ 
    public string Key { get; set; } 
    public int Value { get; set; } 
} 


ObservableCollection<yourclass> dict = new ObservableCollection<MyCustomClass>(); 
dict.Add(new yourclass{Key = "yourkey", Value = whatyouwant}); 
dict.Add(new yourclass{ Key = "yourkey2", Value = whatyouwant }); 
+0

感谢您的帮助。我有这样的课。与关键领域和价值女巫是一个列表类型。但是现在,我在绑定后绝对没有任何东西。也许我可以给你发更多的代码,这样你就可以更好地了解一下吗? – Fixus

+0

嗨看到这个项目,它会帮助你了解如何绑定网格控件http://www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples –

+0

不幸我不能解决我的问题。当我只想绑定项目到网格视图时很容易。但是我不能绑定分组对象。对于带有键的对象和列表作为项目。我什么也没得到。只是空白。没有项目,没有标题:/ – Fixus