【问题标题】:how to create gridview from dictionary如何从字典创建gridview
【发布时间】:2012-08-26 10:40:53
【问题描述】:

我有一个字典,其中键是字符串,元素是列表

我想用 element 中的元素从每个键创建一个组。但是不知道怎么弄

<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 这比第一种方法更好

// 编辑 2 我没有看到项目,因为背景和前景都是白色的。愚蠢的我:) 但现在我有最后一个问题要解决。如何动态设置群组标题?

【问题讨论】:

  • 不要改变你的问题。它使我们向移动的目标射击。
  • @Erno 抱歉,我只是想提供尽可能多的信息
  • 这不是更多信息,这只是另一个问题。想象一下,有些问题与您添加的最后一个问题相同。他偶然发现这个问题的机会有多大?

标签: c# xaml windows-8 windows-runtime winrt-xaml


【解决方案1】:

您实际上不必创建自定义类来启用分组。事实上,您可以使用 LINQ 为您完成:

var 结果 = 从活动组中的行为 act by act.Project 到 grp orderby grp.Key 选择 grp;

cvsActivities.Source = 结果;

为了在 GridView 中显示分组项目,您必须使用 CollectionViewSource。仅将组设置为 GridView.ItemsSource 是行不通的。您必须设置 GridView.ItemsSource = a CollectionViewSource 并且 CollectionViewSource 必须指向组。您可能还必须设置 CollectionViewSource.IsSourceGrouped = true。

【讨论】:

    【解决方案2】:

    Dictionary 没有实现 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 });
    

    【讨论】:

    • 感谢您的帮助。我上了这种课。具有键字段和值的女巫是 List 类型。但是现在绑定后我一无所获。也许我可以为您发送更多代码,以便您有更好的概述?
    • 您好,看看这个项目,它将帮助您了解如何绑定网格控件codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples
    • 不幸的是我可以t solve my problem. When I want to bind only items to grid view its 容易。但我无法绑定分组对象。对于以 key 和 List 作为项目的 exaple 对象。我什么都得不到。只是空白处。没有项目,没有标题:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多