Christos312
Christos312

Reputation: 466

Access TextBlock text in DataTemplate

I am new to C#/XAML coding and I have the following question.

I have this ListView and I have added Aditional Templates to the generated Items

 <ListView x:Name="lvItems" HorizontalAlignment="Left" Height="251" Margin="10,42,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Width="1346" SelectionChanged="lvItems_SelectionChanged" Foreground="{x:Null}" ItemTemplate="{StaticResource Standard500x130ItemTemplate}">

If I go to edit the template I get the following code

<!-- Grid-appropriate 500 by 130 pixel item template as seen in the GroupDetailPage -->
<DataTemplate x:Key="Standard500x130ItemTemplate">
  <Grid Height="110"
        Width="480"
        Margin="10">
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}"
            Width="110"
            Height="110">
      <Image Source="{Binding Image}"
             Stretch="UniformToFill"
             AutomationProperties.Name="{Binding Title}" />
    </Border>
    <StackPanel Grid.Column="1"
                VerticalAlignment="Top"
                Margin="10,0,0,0">
      <TextBlock Text="{Binding Title}"
                 Style="{StaticResource TitleTextStyle}"
                 TextWrapping="NoWrap" />
      <TextBlock Text="{Binding Subtitle}"
                 Style="{StaticResource CaptionTextStyle}"
                 TextWrapping="NoWrap" />
      <TextBlock Text="{Binding Description}"
                 Style="{StaticResource BodyTextStyle}"
                 MaxHeight="60" />
    </StackPanel>
  </Grid>
</DataTemplate>

Now I want to access the Texblocks Title, Subtitle, Description to add data that I have parsed from an XML File. I guess to to that I need to access the Binding of each TextBlock but I have no clue how to do that. Can you help me?

Thanks in advance for your help

Upvotes: 1

Views: 793

Answers (1)

McGarnagle
McGarnagle

Reputation: 102753

You need to set the ItemsSource property of the ListView, and then the fields will populate for each item, based on the template you created.

lvItems.ItemsSource = MyObjectsCollection;

Here I'm assuming MyObjectCollection is a collection of your objects. Judging from your template, the data class should look something like this:

public class TheObject
{
    public string Title { get; set; }
    public string Subtitle { get; set; }
    public string Description { get; set; }
    public string Image { get; set; }
}

So MyObjectsCollection should be an array (or IEnumerable, or List) of TheObject objects.

Upvotes: 2

Related Questions