我有一个我在XAML中构建的Grid/GridView
。我有一个动态构建GridViewItems
的列表。我正在尝试向正在动态构建的GridViewItems
添加元素。我让它经历一个循环并构建GridViewItems很好,我似乎无法掌握如何将元素(TextBlocks,Symblos等)添加到GridViewItems。我将发布以下内容:
XAML
<Grid Background="LightGray">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<controls:PageHeader BackButtonVisibility="Collapsed" Content="News" Frame="{x:Bind Frame}">
<Interactivity:Interaction.Behaviors>
<Behaviors:EllipsisBehavior Visibility="Auto" />
</Interactivity:Interaction.Behaviors>
<controls:PageHeader.SecondaryCommands>
<AppBarButton Click="{x:Bind ViewModel.GotoPrivacy}" Label="Privacy" />
<AppBarButton Click="{x:Bind ViewModel.GotoAbout}" Label="About" />
</controls:PageHeader.SecondaryCommands>
</controls:PageHeader>
<GridView x:Name="tileGridView" Margin="12,60">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Background="#2A2A2A"
Margin="5"
Height="200"
Width="300">
<ContentPresenter />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
</GridView.ItemContainerStyle>
</Grid>
C#
List<TextBlock> tList = new List<TextBlock>();
for (int j = 0; j < myList.Count; j++)
{
tList.Add(new TextBlock()
{
Text = myList[j].TitleView,
Foreground = new SolidColorBrush(Windows.UI.Colors.White)
});
tList.Add(new TextBlock()
{
Text = myList[j].BodyView,
Foreground = new SolidColorBrush(Windows.UI.Colors.White)
});
}
tileGridView.ItemsSource = myList;
我无法找到将titleTextBlock
添加到GridViewItem的方法。在将itemssource设置为myList
之前,不会构建GridViewItem。
有人可以指导我如何将我构建的文本块添加到GridViewItem
吗?
更新
我已将更新添加到我现在的位置...我已成功构建textblock
,但我无法找到将文本块添加到GridViewItem的方法。我的列表现在返回2个对象。这应该构建2个GridViewItems,但是在这2个对象中有4条信息(Title,Body,Author,Date)。我正在尝试构建4个文本块以放置在每个GridViewItem中...希望这更好地解释了我想要实现的目标。
答案 0 :(得分:0)
我能够找到解决方案......这就是我所做的,如果有人知道更好的方法,请随时反驳我的答案。
<DataTemplate x:Key="TileTemplate">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding TitleView}" FontFamily="Segoe UI" FontWeight="SemiBold" FontSize="18" Foreground="White" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10" />
<TextBlock Text="{Binding BodyView}" FontFamily="Segoe UI" FontWeight="Light" FontSize="14" Foreground="White" TextWrapping="Wrap" Margin="10,0" />
</StackPanel>
</DataTemplate>
<GridView x:Name="tileGridView" Margin="12,60" ItemTemplate="{StaticResource TileTemplate}">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GridView>
然后我将数据绑定到gridview:
tileGridView.ItemsSource = myList;