将列表绑定到Gridview

时间:2013-02-02 12:06:59

标签: c# xaml windows-store-apps

我正在努力解决这个问题,我有一个列表集合,我正在尝试绑定到GridView;

  public class GetMenu
    {
        public string titleName { get; set; }
        public string imagePath { get; set; }
    }

    List<GetMenu> Menu = new List<GetMenu>()
    {
        new GetMenu(){titleName = "one", imagePath = "image.jpg"},
        new GetMenu(){titleName = "one", imagePath = "image.jpg"}    
    };

我的XAML代码看起来像;

  <GridView x:Name="MenuViewGrid" TabIndex="1"
        Grid.RowSpan="2" Padding="116,136,116,46" Margin="0,0,50,0" VerticalAlignment="Center" 
        SelectionMode="None" IsSwipeEnabled="false" IsItemClickEnabled="True" ItemClick="ItemView_ItemClick">
        <GridView.ItemTemplate>
            <DataTemplate>
            <Grid HorizontalAlignment="Left" Width="275" Height="425" >
                <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
                        <Image Source="{Binding Path=imagePath}" Stretch="UniformToFill"/>
                </Border>
                <StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
                    <TextBlock Text="{Binding Path=titleName}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" FontSize="25"  Height="60" Margin="15,0,15,0"/>
                </StackPanel>
            </Grid>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>

在我的脑海里,我以为我能够做一个简单的事情;

MenuViewGrid.ItemsSource = GetMenu;

但是当做上述操作时,我收到一条错误消息,说它是一种类型,但我正在尝试将其用作变量。我无法理解这一点,因为我之前在Windows Phone上使用过代码。

1 个答案:

答案 0 :(得分:0)

GetMenu替换为MenuGetMenu是您的班级,Menu是您的列表):

MenuViewGrid.ItemsSource = Menu;