Win 8 App Set text in data template

65 views Asked by At

Im trying to learn how to make datatemplate in listviews in my win 8 app

I have the following code in my Xaml code

<!-- Vertical scrolling item list -->
    <ListView x:Name="itemListView"
      Margin="120,0,0,60"
      ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
      SelectionChanged="ItemListView_SelectionChanged">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid Height="110" Margin="6">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="110" Height="110">
                        <Image Source="{Binding Image}" Stretch="UniformToFill"/>
                    </Border>
                    <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0">
                        <TextBlock Text="{Binding Title}"  TextWrapping="NoWrap" FontFamily="Global User Interface"/>
                        <TextBlock Text="{Binding Subtitle}"  TextWrapping="NoWrap"/>
                        <TextBlock Text="{Binding Description}"  MaxHeight="60" FontFamily="Global User Interface"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

What I cant work out is how to set the text for the three text blocks Title,Subtitle & Description as well as the Picture in Image

Usually When the page loads I use the following in my itemListView_Loaded(object sender, RoutedEventArgs e) method

itemListView.Items.Add(Convert.ToString(correct) + ". " + line.Split(',')[6]);

But how do I do it Im really stumped

Any help appreciated

Mark

1

There are 1 answers

6
AirPett On BEST ANSWER

You'll have to make a class which includes those properties.

public class MyItem
{
    public string Title { get; set; }
    public string Subtitle { get; set; }
    public string Description { get; set; }
    public string Source { get; set; }
}

Then when you add items:

var item = new MyItem();
item.Title = "Title";
item.Subtitle = "Subtitle";
item.Description = "Some example description.";
item.Source = "Assets/SomeFolder/SomeImage.png";
itemListView.Items.Add(item);

That worked in my app (AirPett Transit)