原文:重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView

[源码下载]

重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView

作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之 ListView 和 GridView

  • ListView - 列表控件
  • GridView - 网格控件

示例
1、ListView 的 Demo
ListViewDemo.xaml

<Page
x:Class="XamlDemo.Controls.ListViewDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Page.Resources>
<DataTemplate x:Key="ItemTemplate">
<StackPanel Orientation="Vertical">
<TextBlock TextWrapping="Wrap" FontSize="14.667" Text="{Binding Name}" HorizontalAlignment="Left" />
<TextBlock TextWrapping="Wrap" FontSize="14.667" Text="{Binding Age}" HorizontalAlignment="Left"/>
</StackPanel>
</DataTemplate>
<Style x:Key="ItemContainerStyle" TargetType="ListViewItem">
<Setter Property="Width" Value="292" />
<Setter Property="Height" Value="80" />
<Setter Property="Padding" Value="0" />
<!--
即使将 Margin 设置为“0”,也无法去掉 item 之间的 margin
如果想要去掉 item 之间的 margin,请将此 Margin 属性设置为“-4”
-->
<Setter Property="Margin" Value="0" />
<Setter Property="Background" Value="Blue" />
</Style>
</Page.Resources> <Grid Background="Transparent">
<Grid Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0 30 0 0">
<CheckBox Name="chkIsSwipeEnabled" Content="IsSwipeEnabled" />
<CheckBox Name="chkIsItemClickEnabled" Content="IsItemClickEnabled" Margin="10 0 0 0" />
</StackPanel> <!--后台绑定方式为 ListView 提供数据-->
<ListView x:Name="listView" Width="300" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0 60 10 10" BorderThickness="1" BorderBrush="Red" Background="LightBlue"
ItemTemplate="{StaticResource ItemTemplate}"
ItemContainerStyle="{StaticResource ItemContainerStyle}"
ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"
SelectionMode="Single"
SelectionChanged="listView_SelectionChanged_1"
IsSwipeEnabled="{Binding IsChecked, ElementName=chkIsSwipeEnabled}"
IsItemClickEnabled="{Binding IsChecked, ElementName=chkIsItemClickEnabled}"
ItemClick="listView_ItemClick_1">
</ListView> <!--
xaml 方式为 ListView 添加内容
<ListView>
<ListView.Items>
<ListViewItem>
...
</ListViewItem>
<ListViewItem>
...
</ListViewItem>
...
</ListView.Items>
</ListView>
-->
</Grid>
</Grid>
</Page>

ListViewDemo.xaml.cs

/*
* ListView - 列表控件
* IsItemClickEnabled - item 是否可被点击
* IsSwipeEnabled - 是否支持 swipe 操作(对于 ListView 来说,左右猛击 item 称之为 swipe)
* SelectionMode - item 的选中模式(Windows.UI.Xaml.Controls.ListViewSelectionMode 枚举)
* None - 不能被选中
* Single - 只能单选
* Multiple - 仅通过鼠标多选
* Extended - 通过鼠标和辅助键多选(ctrl 或 shift)
* SelectedItems - 被选中的 items 集合
* ItemClick - item 被单击时触发的事件
* SelectAll() - 选中全部 items
* ScrollIntoView(object item, ScrollIntoViewAlignment alignment) - 滚动到指定的 item
* ScrollIntoViewAlignment.Default - 与该 item 的最近边缘对齐
* ScrollIntoViewAlignment.Leading - 与该 item 的前边缘对齐
*
*
* 注:
* IsItemClickEnabled == false && IsSwipeEnabled == false 无法响应单击事件,单击则意味着选中,无法 swipe
* IsItemClickEnabled == true && IsSwipeEnabled == false 可以响应单击事件,无法响应选中事件,无法 swipe
* IsItemClickEnabled == false && IsSwipeEnabled == true 无法响应单击事件,单击和 swipe 均意味着选中
* IsItemClickEnabled == true && IsSwipeEnabled == true 可以响应单击事件,swipe 则意味着选中
*
* 关于 SemanticZoom, item的拖动, item的尺寸可变等之后通过 GridView 来介绍
*
* 关于分页加载内容在“数据绑定”一节做介绍
*/ using System.Collections.Generic;
using Windows.UI.Xaml.Controls;
using XamlDemo.Model; namespace XamlDemo.Controls
{
public sealed partial class ListViewDemo : Page
{
public ListViewDemo()
{
this.InitializeComponent(); // 绑定数据
List<Employee> dataSource = TestData.GetEmployees();
listView.ItemsSource = dataSource;
} // 单击行为的事件
private void listView_ItemClick_1(object sender, ItemClickEventArgs e)
{
lblMsg.Text = "被单击的 employee 的 name 为:" + (e.ClickedItem as Employee).Name;
} // 选中行为的事件
private void listView_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > )
lblMsg.Text = "此次操作被选中的 employee 的 name 为:" + (e.AddedItems[] as Employee).Name;
else
lblMsg.Text = "此次操作没有被选中的 employee";
}
}
}

2、GridView 的 Demo
GridView/Demo.xaml

<Page
x:Class="XamlDemo.Controls.GridView.Demo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls.GridView"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Page.Resources>
<DataTemplate x:Key="ItemTemplate">
<StackPanel Orientation="Vertical">
<TextBlock TextWrapping="Wrap" FontSize="14.667" Text="{Binding Name}" HorizontalAlignment="Left" />
<TextBlock TextWrapping="Wrap" FontSize="14.667" Text="{Binding Age}" HorizontalAlignment="Left"/>
</StackPanel>
</DataTemplate>
<Style x:Key="ItemContainerStyle" TargetType="GridViewItem">
<Setter Property="Width" Value="292" />
<Setter Property="Height" Value="80" />
<!--
即使将 Margin 设置为“0”,也无法去掉 item 之间的 margin
如果想要去掉 item 之间的 margin,请将此 Margin 属性设置为“-4”
-->
<Setter Property="Margin" Value="0" />
<Setter Property="Background" Value="Blue" />
</Style>
<ItemsPanelTemplate x:Key="ItemsPanel">
<!--
注:WrapGrid 继承自 VirtualizingPanel,而 VariableSizedWrapGrid 并未继承 VirtualizingPanel
-->
<WrapGrid MaximumRowsOrColumns="3" Orientation="Vertical" VerticalChildrenAlignment="Top" HorizontalChildrenAlignment="Left" />
</ItemsPanelTemplate>
</Page.Resources> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <StackPanel Orientation="Horizontal" Margin="0 10 0 0">
<CheckBox Name="chkIsSwipeEnabled" Content="IsSwipeEnabled" />
<CheckBox Name="chkIsItemClickEnabled" Content="IsItemClickEnabled" Margin="10 0 0 0" />
</StackPanel> <!--后台绑定方式为 ListView 提供数据-->
<GridView x:Name="gridView" VerticalAlignment="Top" Margin="0 10 10 0" BorderThickness="1" BorderBrush="Red" Background="LightBlue"
ItemTemplate="{StaticResource ItemTemplate}"
ItemContainerStyle="{StaticResource ItemContainerStyle}"
ItemsPanel="{StaticResource ItemsPanel}"
ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"
SelectionMode="Single"
SelectionChanged="gridView_SelectionChanged_1"
IsSwipeEnabled="{Binding IsChecked, ElementName=chkIsSwipeEnabled}"
IsItemClickEnabled="{Binding IsChecked, ElementName=chkIsItemClickEnabled}"
ItemClick="gridView_ItemClick_1">
</GridView> <!--
xaml 方式为 ListView 添加内容
<GridView>
<GridView.Items>
<GridViewItem>
...
</GridViewItem>
<GridViewItem>
...
</GridViewItem>
...
</GridView.Items>
</GridView>
-->
</StackPanel>
</Grid>
</Page>

GridView/Demo.xaml.cs

/*
* GridView - 网格控件
* IsItemClickEnabled - item 是否可被点击
* IsSwipeEnabled - 是否支持 swipe 操作(对于 GridView 来说,上下猛击 item 称之为 swipe)
* SelectionMode - item 的选中模式(Windows.UI.Xaml.Controls.ListViewSelectionMode 枚举)
* None - 不能被选中
* Single - 只能单选
* Multiple - 仅通过鼠标多选
* Extended - 通过鼠标和辅助键多选(ctrl 或 shift)
* SelectedItems - 被选中的 items 集合
* ItemClick - item 被单击时触发的事件
* SelectAll() - 选中全部 items
* ScrollIntoView(object item, ScrollIntoViewAlignment alignment) - 滚动到指定的 item
* ScrollIntoViewAlignment.Default - 与该 item 的最近边缘对齐
* ScrollIntoViewAlignment.Leading - 与该 item 的前边缘对齐
*
*
* 注:
* IsItemClickEnabled == false && IsSwipeEnabled == false 无法响应单击事件,单击则意味着选中,无法 swipe
* IsItemClickEnabled == true && IsSwipeEnabled == false 可以响应单击事件,无法响应选中事件,无法 swipe
* IsItemClickEnabled == false && IsSwipeEnabled == true 无法响应单击事件,单击和 swipe 均意味着选中
* IsItemClickEnabled == true && IsSwipeEnabled == true 可以响应单击事件,swipe 则意味着选中
*/ using System.Collections.Generic;
using Windows.UI.Xaml.Controls;
using XamlDemo.Model; namespace XamlDemo.Controls.GridView
{
public sealed partial class Demo : Page
{
public Demo()
{
this.InitializeComponent(); // 绑定数据
List<Employee> dataSource = TestData.GetEmployees();
gridView.ItemsSource = dataSource;
} // 单击行为的事件
private void gridView_ItemClick_1(object sender, ItemClickEventArgs e)
{
lblMsg.Text = "被单击的 employee 的 name 为:" + (e.ClickedItem as Employee).Name;
} // 选中行为的事件
private void gridView_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > )
lblMsg.Text = "此次操作被选中的 employee 的 name 为:" + (e.AddedItems[] as Employee).Name;
else
lblMsg.Text = "此次操作没有被选中的 employee";
}
}
}

OK
[源码下载]

重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView的更多相关文章

  1. 重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding

    原文:重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding [源码下 ...

  2. 重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree

    原文:重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree [源码下载] 重新想象 ...

  3. 重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState, VisualStateManager

    原文:重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState ...

  4. 重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试

    原文:重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试 [源码下载] 重新想象 Windows 8 Store ...

  5. 重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom

    原文:重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom [源码下载] 重新想象 Windows 8 Store Apps (13) - 控件之 Sem ...

  6. 重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示

    原文:重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示 [源码下载] 重新想象 Windows 8 Store Ap ...

  7. 重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom

    原文:重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom [源码下载] ...

  8. 重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础

    原文:重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础 [源码下载] 重新想象 Windows 8 Store Apps (9) - 控件之 Sc ...

  9. 重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGrid, VariableSizedWrapGrid

    原文:重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGr ...

随机推荐

  1. java web从零单排第十六期《struts2》控制标签(2)

    1.s:subset标签概述: s:subset标签功能是从一个集合中取出部分元素合并成一个新的集合,新生成的这个集合是原来集合的子集.属性和意义如下: 属性名 是否必需 默认值 类型 说明介绍 co ...

  2. Delphi基础Write写入结构体到文件(使用 file of myrecord就行了,真简单)

    program WriteStruct; {$APPTYPE CONSOLE} uses SysUtils; //写入结构体 type TCustomer = record ID: ]; Code: ...

  3. [Android学习笔记]使用ListView

    简单使用ListView 关键在于Adatper Adatper用来连接UI与数据源.Adapter既负责提供数据,又负责创建Item视图. 一般步骤: 1.创建list_item.xml,用来创建L ...

  4. 算法 《秦九韶算法java实践》

    [历史背景] 秦九韶算法是中国南宋时期的数学家秦九韶表述求解一元高次多项式的值的算法--正负开方术.它也能够配合牛顿法用来求解一元高次多项式的根.在西方被称作霍纳算法(Horner algorithm ...

  5. DJ_Java_Decompiler新手入门教程

    首先声明:这篇文章并不是我原创,只是感觉挺有用处,想跟大家分享一下,所以标注为原创,希望能有更多的朋友可以看到,还请原作者谅解. 昨天大D说让我写下DJ入门的基础,今天写了一大半了,结果不小心把浏览器 ...

  6. Android 推断当前的界面是否是桌面的方法

    在开发桌面飘浮控件的时候,须要通过service查看当前是不是桌面,从而控制漂浮窗的显现与消失,以下的代码就是推断是否是桌面的方法 /** * 推断当前界面是否是桌面 */ private boole ...

  7. libevent安装总结 - jinfg2008的专栏 - 博客频道 - CSDN.NET

    libevent安装总结 - jinfg2008的专栏 - 博客频道 - CSDN.NET libevent安装总结 分类: linux 系统配置 2013-02-13 22:37 99人阅读 评论( ...

  8. mysql 服务启动报1607 error

    [问题说明] mysql曾经还是好好的,突然就不行了...不知道是否使用了腾讯C盘搬家工具引起的... watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2h ...

  9. Urban Dictionary: psd

    Urban Dictionary: psd psd Share on twitter Share on facebook Share on more 3 up, 1 down It means Poo ...

  10. CrossBridge介绍

    CrossBridge介绍 作者:chszs,转载需注明.博客主页: http://blog.csdn.net/chszs CrossBridge是Adobe FlasCC的开源版本,它提供了一个完整 ...