Warm tip: This article is reproduced from serverfault.com, please click

How to bind Gridview with Dictionary in UWP?

发布于 2020-12-21 07:19:52

I want to use Dictionary to bind the data to xaml, I mean repeat the textbox and gridview element using Dictionary. I want to group it over time, please help me!

You can see image:

enter image description here

Code Xaml

<Page.Resources>
    <local:CustomDataObject x:Key="customData"/>
</Page.Resources>
<StackPanel VerticalAlignment="Top">
    <TextBlock Margin="10" FontSize="50">4月2020</TextBlock>
    <GridView SelectionMode="Single" ItemsSource="{StaticResource customData}" Margin="10">
        <GridView.ItemTemplate>
            <DataTemplate>
                <Image Source="{Binding ImageLocation}" Height="180" Width="180" Stretch="UniformToFill"/>
            </DataTemplate>
        </GridView.ItemTemplate>
        <GridView.ItemsPanel>
            <ItemsPanelTemplate>
                <ItemsWrapGrid  Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </GridView.ItemsPanel>
    </GridView>
</StackPanel>

Code C#

public class CustomDataObject : List<ImageData>
{

    public CustomDataObject()
    {
        for (int i = 0; i < 10; i++)
        {
            this.Add(new ImageData()
            {
                Title = "Title",
                ImageLocation = "Assets/math.jpg",
                Views = "Views",
                Likes = "Likes",
                Description = "Description"
            });
        }
    }
    public class ImageData
    {
        public string Title { get; set; }
        public string ImageLocation { get; set; }
        public string Views { get; set; }
        public string Likes { get; set; }
        public string Description { get; set; }
    }

}
Questioner
dungtran
Viewed
0
Nico Zhu - MSFT 2020-12-21 16:51:57

How to bind Gridview with Dictionary in UWP?

For your Dictionary key-value pairs, you need make GridView nested in GridView like the following. And use CollectionViewSource to group the DataSource with key.

Xaml

<Grid.Resources>
    <CollectionViewSource x:Key="cvs" x:Name="cvs" />
</Grid.Resources>
<GridView
    x:Name="MyListView"
    IsItemClickEnabled="False"
    ItemsSource="{Binding Source={StaticResource cvs}}">
    <GridView.ItemTemplate>
        <DataTemplate>
            <GridView ItemsSource="{Binding Value}">
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <Image
                            Width="100"
                            Height="100"
                            Source="{Binding}" />
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>
        </DataTemplate>
    </GridView.ItemTemplate>
    <GridView.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Key}" />
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </GridView.GroupStyle>
</GridView>

Code

private void MessagePage_Loaded(object sender, RoutedEventArgs e)
{
    var list = new List<string>() { "ms-appx:///Assets/img.png",
        "ms-appx:///Assets/img.png" ,
        "ms-appx:///Assets/img.png" ,
        "ms-appx:///Assets/img.png" };

    var items = new Dictionary<string, List<string>>() { { "Hello", list }, { "Some", list }, { "To", list } };   
    var groups = from c in items
                 group c by c.Key;

    this.cvs.Source = groups;
    this.cvs.IsSourceGrouped = true;
}