Adding TextBlock to GridViewItem c#

2019-09-02 17:01发布

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.

标签: c# wpf xaml grid
1条回答
一纸荒年 Trace。
2楼-- · 2019-09-02 17:54

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;
查看更多
登录 后发表回答