Search code examples
windowsgridviewwindows-8windows-store-appsdatatemplate

Windows Store App, Grid View items not displaying


i'm working on a windows 8 app but i'm new to WPF. I've spend a good day on this but can't figure out why it doesn't work. Basically I am trying to create a grid view of some images with text at the top. But only a gradient filled box is displayed, no text or image.

Below is my page which at this moment is very simple. The image has been added to the project and lives in the Assets folder.

Can anyone please tell me why it doesn't work.

<Page
x:Class="App4.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App4"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"
    <GridView 
        x:Name="GridView1"
        Margin="0" ItemsSource="{Binding}" 
        AutomationProperties.AutomationId="GridView1"
        AutomationProperties.Name="Items">
        <GridView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <StackPanel.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="White"/>
                            <GradientStop Color="#FF443585" Offset="1"/>
                        </LinearGradientBrush>
                    </StackPanel.Background>
                    <TextBlock Text="{Binding ItemName}" FontSize="25" Padding="10" Visibility="Visible" Foreground="Red"/>
                    <Image Source="Logo.png" Height="100" Width="250" Visibility="Visible"/>
                </StackPanel>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>

</Grid>

    private void Page_Loaded_1(object sender, RoutedEventArgs e)
    {
        List<Item> Temp = new List<Item>() { new Item() { ItemName = "test" }, new Item() { ItemName = "test 2" } };

        GridView1.DataContext = Temp;
    }

Thanks


Solution

  • you need to define ItemsSource for GridView

    GridView1.ItemsSource = Temp;