wp8 LongListSelector简单使用

ui设置

<phone:PhoneApplicationPage
    x:Class="LongListSelector.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
    <phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="dt">
            <StackPanel>
                <TextBlock Text="{Binding Title}" Height="80"></TextBlock>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="GroupTemplate">
            <Border Background="Red" Height="80">
                <TextBlock Text="{Binding Key}" FontSize="70"/>
            </Border>
        </DataTemplate>
        <Style x:Key="style1" TargetType="phone:LongListSelector">
            <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Border Background="Red" Height="80">
                            <TextBlock Text="{Binding Key}" FontSize="70"/>
                        </Border>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </phone:PhoneApplicationPage.Resources>
    <!--LayoutRoot 是包含所有页面内容的根网格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
            <TextBlock Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
        <!--ContentPanel - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <phone:LongListSelector 
                x:Name="longls" 
                ItemTemplate="{StaticResource dt}" 
                IsGroupingEnabled="True"
                LayoutMode="List"
                GroupHeaderTemplate="{StaticResource GroupTemplate}"
                JumpListStyle="{StaticResource style1}">
            </phone:LongListSelector>
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using LongListSelector.Resources;

namespace LongListSelector
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            //定义List泛形数组
            List<Item> mianItem = new List<Item>();
            //添加Item内容
            for (int i = 0; i < 10; i++)
            {
                mianItem.Add(new Item{Content = "A类别",Title="Test A"+i});
                mianItem.Add(new Item { Content = "B类别", Title = "Test B" + i });
                mianItem.Add(new Item { Content = "C类别", Title = "Test C" + i });
            }
            var Items = (from item in mianItem group item by item.Content into newItems select new ItemInGroup<Item>(newItems.Key, newItems.ToList())).ToList();
            longls.ItemsSource = Items;

        }
        public class ItemInGroup<T> : List<T>
        {
            public string Key { get; set; }
            public ItemInGroup(string key, List<T> items)
            {
                Key = key;
                this.AddRange(items);
            }
        }
        class Item
        {
            public string Title { get; set; }
            public string Content { get; set; }
        }

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值