Я хочу использовать словарь для привязки данных к xaml, я имею в виду повторение текстового поля и элемента gridview с помощью словаря. Я хочу сгруппировать его по времени, пожалуйста, помогите мне!
Вы можете увидеть изображение:
Код 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>
Код С#
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; }
}
}
@NicoZhu-MSFT, вы можете использовать значение ключа «key1» List<ImageData> ...
Хорошо, я вас понял, я сделаю решение.
Спасибо тебе большое, я тебя жду
Как связать Gridview со словарем в UWP?
Для пар ключ-значение словаря вам нужно сделать GridView вложенным в GridView, как показано ниже. И используйте CollectionViewSource, чтобы сгруппировать DataSource с ключом.
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>
Код
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;
}
Привет, ты можешь дать мне полный код или github
Не могли бы вы поделиться своими парами ключ-значение в словаре?