将TextBlock添加到GridViewItem c# [英] Adding TextBlock to GridViewItem c#
问题描述
我有一个在XAML中构建的 Grid / GridView
。我有一个动态构建 GridViewItems
的列表。我正在尝试将元素添加到正被动态构建的 GridViewItems
中。我已经通过一个循环并构建了GridViewItems,我似乎无法掌握如何将元素(TextBlocks,Symblos等)添加到GridViewItems。我将发布以下内容:
XAML
< Grid Background =LightGray>
< Grid.RowDefinitions>
< RowDefinition Height =Auto/>
< RowDefinition />
< /Grid.RowDefinitions>
< controls:PageHeader BackButtonVisibility =CollapsedContent =NewsFrame ={x:Bind Frame}>
< Interactivity:Interaction.Behaviors>
<行为:EllipsisBehavior Visibility =Auto/>
< 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 =tileGridViewMargin =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>
< /Style.Setters>
< / style>
< /GridView.ItemContainerStyle>
< / Grid>
C#
列表< TextBlock> tList = new List< TextBlock>();
for(int j = 0; 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已将我的更新添加到我现在的位置......我已成功构建了 textblock
,但我一直无法找到将文本块添加到GridViewItem的方法。我的列表现在返回2个对象。这应该构建2个GridViewItems,它可以实现,但在这两个对象内部有4条信息(标题,正文,作者,日期)。我试图建立4个文本块放置在每个GridViewItem中...希望这解释了我所要完成的更好。
我能够找出解决方案......这就是我所做的,如果有人知道更好的方法做到这一点,请随时反驳我的答案。
< DataTemplate x:Key =TileTemplate>
< StackPanel Orientation =Vertical>
< TextBlock Text ={Binding TitleView}FontFamily =Segoe UIFontWeight =SemiBoldFontSize =18Foreground =WhiteTextWrapping =WrapHorizontalAlignment =LeftVerticalAlignment =Top Margin =10,10/>
< TextBlock Text ={Binding BodyView}FontFamily =Segoe UIFontWeight =LightFontSize =14Foreground =WhiteTextWrapping =WrapMargin =10,0/> ;
< / StackPanel>
< / DataTemplate>
< GridView x:Name =tileGridViewMargin =12,60ItemTemplate ={StaticResource TileTemplate}>
< GridView.ItemsPanel>
< ItemsPanelTemplate>
< ItemsWrapGrid Orientation =Horizontal/>
< / ItemsPanelTemplate>
< / GridView>
然后将数据绑定到gridview:
tileGridView.ItemsSource = myList;
I have a a Grid/GridView
that I have built in XAML. I have a list that is dynamically building the GridViewItems
. I am trying to add elements to the GridViewItems
that are being dynamically built. I have it going through a loop and building the GridViewItems fine, I just can't seem to grasp how to add the elements (TextBlocks, Symblos, etc.) to the GridViewItems. I will post what I have below:
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;
I am unable to figure out a way to add the titleTextBlock
to the GridViewItem. The GridViewItem is not being built until I set the itemssource to myList
.
Can anyone guide me on how to add the textblock I have built to my GridViewItem
?
UPDATE
I have added my update to where I am now... I have successfully built the textblock
, but I have not been able to find a way to add the textblocks to a GridViewItem. My List returns 2 objects right now. That should build 2 GridViewItems, which it does, but inside of those 2 objects there are 4 pieces of information(Title, Body, Author, Date). I am trying to build 4 textblocks to place in each GridViewItem... Hope this explains better of what I am trying to accomplish.
I was able to figure the solution out... This is what I did, if anyone knows of a better way to do this, please feel free to counter my answer.
<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>
I then bind the data to the gridview:
tileGridView.ItemsSource = myList;
这篇关于将TextBlock添加到GridViewItem c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!