【问题标题】:Binding list of items on Windows phoneWindows phone 上的项目绑定列表
【发布时间】:2014-01-24 23:36:41
【问题描述】:

我正在学习 windows phone 上的数据绑定,到目前为止,我已经能够将单个对象绑定到应用程序的可视端,但现在我试图了解如何根据数字创建可视元素我在列表中的对象

我有一个人的课程

public class Person
{
    public Person() { }
    public string name { get; set; } 
    public string fotoPath { get; set; }
}

我有一个人集合类

public class PersonCollection
{
    public PersonCollection() { }
    public List<Person> personGroup { get; set; } 
}

然后我有我的页面代码,在那里我生成我的人员列表

    public partial class TestPage : PhoneApplicationPage
    {
      public TestPage()
      {
        InitializeComponent();
        Loaded += TestPage_Loaded;
      }

    void TestPage_Loaded(object sender, RoutedEventArgs e)
    {
        PersonCollection lista = new PersonCollection();
        lista.personGroup.Add(new Person(){name = "Mr.T", fotoPath = "/images/foto1.jpg"});
        lista.personGroup.Add(new Person(){name = "John", fotoPath = "/images/foto2.jpg"});

    }
}

在我的页面上,我希望有一个网格,在每个单元格上显示一张照片和人名,用于我列表中的每个人(每行 2 人)。据我了解,我将需要使用 DataTemplate,但现在我的努力失败了。 谁能给我一些指点?

【问题讨论】:

    标签: c# xaml data-binding windows-phone-8 windows-phone


    【解决方案1】:

    以下是每行展示 2 个人的方法。首先,将源集合分成 2 个一组:

    List<Tuple<Person, Person>> groupedItems = lista.personGroup
        .GroupBy(item => lista.personGroup.IndexOf(item) / 2)
        .Select(grp => new Tuple<Person, Person>(grp.First(), grp.Skip(1).FirstOrDefault()))
        .ToList();
    items.ItemsSource = groupedItems;
    

    现在使用DataTemplate,在左右列中显示“Item1”和“Item2”:

    <ItemsControl x:Name="items"> 
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.Resources>
                        <DataTemplate x:Key="ColumnTemplate">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding name}" Width="150" />
                                <Image Source="{Binding fotoPath}" />
                            </StackPanel>
                        </DataTemplate>
                    </Grid.Resources>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <ContentControl Content="{Binding Item1}" 
                                    ContentTemplate="{StaticResource ColumnTemplate}" />
                    <ContentControl Grid.Column="1" 
                                    Content="{Binding Item2}" 
                                    ContentTemplate="{StaticResource ColumnTemplate}" />
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    【讨论】:

    • 啊,我明白了,谢谢你的帮助。有了这个我会得到一个人每行正确吗?我将如何更改它,以便每行有 2 个人,所以它看起来像 [name][photo] [name][photo] 然后又像更改 [name][photo] [name][photo]?
    • @Ric 2 每行实际上有点棘手......您需要使用自定义面板,或者使用带有 hack 的 WrapPanel(在 WP 工具包中)以确保项目是屏幕大小的 1/2,或使用代码隐藏将项目放入 Grid ... 或(可能是最简单的)将项目本身分组为 2 个组。
    • @Ric 我已经更新了我上面的答案,使用最后一个建议——将源集合聚合成 2 个组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 2014-08-30
    相关资源
    最近更新 更多