如何在Windows Phone 7中创建一个包含名称的简单表?

时间:2011-08-31 13:12:54

标签: .net windows-phone-7

我想要一个包含小图像和名称的简单表格,这样当我点击一个单元格/项目时会显示另一个视图?我怎样才能做到这一点?我有一些iphone开发和.NET的经验,但我是WP 7的新手,谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用ListBox执行此操作,如下所示:

<ListBox ItemsSource="{Binding Items}"
         SelectionChanged="OnListSelectionChanged">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
        <Image Source="{Binding ImageSource}" />
        <TextBlock Text="{Binding ItemName}" />
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

这将显示一个列表或表格,其中包含图像和一些文本。您的视图模型需要声明某种Item个对象的集合(List<Items>如果它是静态的,或ObservableCollection<Items>如果可以添加/删除的话)。每个Item必须至少包含2个公共属性ImageSource(包含图像路径的字符串)&amp; ItemName(包含说明的字符串)。

OnListSelectionChanged处理程序中执行此操作

var item = (sender as ListBox).SelectedItem as Item;
// you can now access item.ItemName or item.ImageSource or other properties of Item

数据绑定是一个很大的主题,在尝试编写应用程序之前,您应该了解更多相关信息。这是一个MSDN article来处理基础知识。

特别是对于Windows Phone开发,我建议您下载Charles Petzold的free book并阅读。