原文:重新想象 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

  1. <Page
  2. x:Class="XamlDemo.Controls.ListViewDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:XamlDemo.Controls"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Page.Resources>
  11. <DataTemplate x:Key="ItemTemplate">
  12. <StackPanel Orientation="Vertical">
  13. <TextBlock TextWrapping="Wrap" FontSize="14.667" Text="{Binding Name}" HorizontalAlignment="Left" />
  14. <TextBlock TextWrapping="Wrap" FontSize="14.667" Text="{Binding Age}" HorizontalAlignment="Left"/>
  15. </StackPanel>
  16. </DataTemplate>
  17. <Style x:Key="ItemContainerStyle" TargetType="ListViewItem">
  18. <Setter Property="Width" Value="292" />
  19. <Setter Property="Height" Value="80" />
  20. <Setter Property="Padding" Value="0" />
  21. <!--
  22. 即使将 Margin 设置为“0”,也无法去掉 item 之间的 margin
  23. 如果想要去掉 item 之间的 margin,请将此 Margin 属性设置为“-4”
  24. -->
  25. <Setter Property="Margin" Value="0" />
  26. <Setter Property="Background" Value="Blue" />
  27. </Style>
  28. </Page.Resources>
  29.  
  30. <Grid Background="Transparent">
  31. <Grid Margin="120 0 0 0">
  32.  
  33. <TextBlock Name="lblMsg" FontSize="14.667" />
  34.  
  35. <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0 30 0 0">
  36. <CheckBox Name="chkIsSwipeEnabled" Content="IsSwipeEnabled" />
  37. <CheckBox Name="chkIsItemClickEnabled" Content="IsItemClickEnabled" Margin="10 0 0 0" />
  38. </StackPanel>
  39.  
  40. <!--后台绑定方式为 ListView 提供数据-->
  41. <ListView x:Name="listView" Width="300" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0 60 10 10" BorderThickness="1" BorderBrush="Red" Background="LightBlue"
  42. ItemTemplate="{StaticResource ItemTemplate}"
  43. ItemContainerStyle="{StaticResource ItemContainerStyle}"
  44. ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"
  45. SelectionMode="Single"
  46. SelectionChanged="listView_SelectionChanged_1"
  47. IsSwipeEnabled="{Binding IsChecked, ElementName=chkIsSwipeEnabled}"
  48. IsItemClickEnabled="{Binding IsChecked, ElementName=chkIsItemClickEnabled}"
  49. ItemClick="listView_ItemClick_1">
  50. </ListView>
  51.  
  52. <!--
  53. xaml 方式为 ListView 添加内容
  54. <ListView>
  55. <ListView.Items>
  56. <ListViewItem>
  57. ...
  58. </ListViewItem>
  59. <ListViewItem>
  60. ...
  61. </ListViewItem>
  62. ...
  63. </ListView.Items>
  64. </ListView>
  65. -->
  66. </Grid>
  67. </Grid>
  68. </Page>

ListViewDemo.xaml.cs

  1. /*
  2. * ListView - 列表控件
  3. * IsItemClickEnabled - item 是否可被点击
  4. * IsSwipeEnabled - 是否支持 swipe 操作(对于 ListView 来说,左右猛击 item 称之为 swipe)
  5. * SelectionMode - item 的选中模式(Windows.UI.Xaml.Controls.ListViewSelectionMode 枚举)
  6. * None - 不能被选中
  7. * Single - 只能单选
  8. * Multiple - 仅通过鼠标多选
  9. * Extended - 通过鼠标和辅助键多选(ctrl 或 shift)
  10. * SelectedItems - 被选中的 items 集合
  11. * ItemClick - item 被单击时触发的事件
  12. * SelectAll() - 选中全部 items
  13. * ScrollIntoView(object item, ScrollIntoViewAlignment alignment) - 滚动到指定的 item
  14. * ScrollIntoViewAlignment.Default - 与该 item 的最近边缘对齐
  15. * ScrollIntoViewAlignment.Leading - 与该 item 的前边缘对齐
  16. *
  17. *
  18. * 注:
  19. * IsItemClickEnabled == false && IsSwipeEnabled == false 无法响应单击事件,单击则意味着选中,无法 swipe
  20. * IsItemClickEnabled == true && IsSwipeEnabled == false 可以响应单击事件,无法响应选中事件,无法 swipe
  21. * IsItemClickEnabled == false && IsSwipeEnabled == true 无法响应单击事件,单击和 swipe 均意味着选中
  22. * IsItemClickEnabled == true && IsSwipeEnabled == true 可以响应单击事件,swipe 则意味着选中
  23. *
  24. * 关于 SemanticZoom, item的拖动, item的尺寸可变等之后通过 GridView 来介绍
  25. *
  26. * 关于分页加载内容在“数据绑定”一节做介绍
  27. */
  28.  
  29. using System.Collections.Generic;
  30. using Windows.UI.Xaml.Controls;
  31. using XamlDemo.Model;
  32.  
  33. namespace XamlDemo.Controls
  34. {
  35. public sealed partial class ListViewDemo : Page
  36. {
  37. public ListViewDemo()
  38. {
  39. this.InitializeComponent();
  40.  
  41. // 绑定数据
  42. List<Employee> dataSource = TestData.GetEmployees();
  43. listView.ItemsSource = dataSource;
  44. }
  45.  
  46. // 单击行为的事件
  47. private void listView_ItemClick_1(object sender, ItemClickEventArgs e)
  48. {
  49. lblMsg.Text = "被单击的 employee 的 name 为:" + (e.ClickedItem as Employee).Name;
  50. }
  51.  
  52. // 选中行为的事件
  53. private void listView_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
  54. {
  55. if (e.AddedItems.Count > )
  56. lblMsg.Text = "此次操作被选中的 employee 的 name 为:" + (e.AddedItems[] as Employee).Name;
  57. else
  58. lblMsg.Text = "此次操作没有被选中的 employee";
  59. }
  60. }
  61. }

2、GridView 的 Demo
GridView/Demo.xaml

  1. <Page
  2. x:Class="XamlDemo.Controls.GridView.Demo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:XamlDemo.Controls.GridView"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Page.Resources>
  11. <DataTemplate x:Key="ItemTemplate">
  12. <StackPanel Orientation="Vertical">
  13. <TextBlock TextWrapping="Wrap" FontSize="14.667" Text="{Binding Name}" HorizontalAlignment="Left" />
  14. <TextBlock TextWrapping="Wrap" FontSize="14.667" Text="{Binding Age}" HorizontalAlignment="Left"/>
  15. </StackPanel>
  16. </DataTemplate>
  17. <Style x:Key="ItemContainerStyle" TargetType="GridViewItem">
  18. <Setter Property="Width" Value="292" />
  19. <Setter Property="Height" Value="80" />
  20. <!--
  21. 即使将 Margin 设置为“0”,也无法去掉 item 之间的 margin
  22. 如果想要去掉 item 之间的 margin,请将此 Margin 属性设置为“-4”
  23. -->
  24. <Setter Property="Margin" Value="0" />
  25. <Setter Property="Background" Value="Blue" />
  26. </Style>
  27. <ItemsPanelTemplate x:Key="ItemsPanel">
  28. <!--
  29. 注:WrapGrid 继承自 VirtualizingPanel,而 VariableSizedWrapGrid 并未继承 VirtualizingPanel
  30. -->
  31. <WrapGrid MaximumRowsOrColumns="3" Orientation="Vertical" VerticalChildrenAlignment="Top" HorizontalChildrenAlignment="Left" />
  32. </ItemsPanelTemplate>
  33. </Page.Resources>
  34.  
  35. <Grid Background="Transparent">
  36. <StackPanel Margin="120 0 0 0">
  37.  
  38. <TextBlock Name="lblMsg" FontSize="14.667" />
  39.  
  40. <StackPanel Orientation="Horizontal" Margin="0 10 0 0">
  41. <CheckBox Name="chkIsSwipeEnabled" Content="IsSwipeEnabled" />
  42. <CheckBox Name="chkIsItemClickEnabled" Content="IsItemClickEnabled" Margin="10 0 0 0" />
  43. </StackPanel>
  44.  
  45. <!--后台绑定方式为 ListView 提供数据-->
  46. <GridView x:Name="gridView" VerticalAlignment="Top" Margin="0 10 10 0" BorderThickness="1" BorderBrush="Red" Background="LightBlue"
  47. ItemTemplate="{StaticResource ItemTemplate}"
  48. ItemContainerStyle="{StaticResource ItemContainerStyle}"
  49. ItemsPanel="{StaticResource ItemsPanel}"
  50. ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"
  51. SelectionMode="Single"
  52. SelectionChanged="gridView_SelectionChanged_1"
  53. IsSwipeEnabled="{Binding IsChecked, ElementName=chkIsSwipeEnabled}"
  54. IsItemClickEnabled="{Binding IsChecked, ElementName=chkIsItemClickEnabled}"
  55. ItemClick="gridView_ItemClick_1">
  56. </GridView>
  57.  
  58. <!--
  59. xaml 方式为 ListView 添加内容
  60. <GridView>
  61. <GridView.Items>
  62. <GridViewItem>
  63. ...
  64. </GridViewItem>
  65. <GridViewItem>
  66. ...
  67. </GridViewItem>
  68. ...
  69. </GridView.Items>
  70. </GridView>
  71. -->
  72. </StackPanel>
  73. </Grid>
  74. </Page>

GridView/Demo.xaml.cs

  1. /*
  2. * GridView - 网格控件
  3. * IsItemClickEnabled - item 是否可被点击
  4. * IsSwipeEnabled - 是否支持 swipe 操作(对于 GridView 来说,上下猛击 item 称之为 swipe)
  5. * SelectionMode - item 的选中模式(Windows.UI.Xaml.Controls.ListViewSelectionMode 枚举)
  6. * None - 不能被选中
  7. * Single - 只能单选
  8. * Multiple - 仅通过鼠标多选
  9. * Extended - 通过鼠标和辅助键多选(ctrl 或 shift)
  10. * SelectedItems - 被选中的 items 集合
  11. * ItemClick - item 被单击时触发的事件
  12. * SelectAll() - 选中全部 items
  13. * ScrollIntoView(object item, ScrollIntoViewAlignment alignment) - 滚动到指定的 item
  14. * ScrollIntoViewAlignment.Default - 与该 item 的最近边缘对齐
  15. * ScrollIntoViewAlignment.Leading - 与该 item 的前边缘对齐
  16. *
  17. *
  18. * 注:
  19. * IsItemClickEnabled == false && IsSwipeEnabled == false 无法响应单击事件,单击则意味着选中,无法 swipe
  20. * IsItemClickEnabled == true && IsSwipeEnabled == false 可以响应单击事件,无法响应选中事件,无法 swipe
  21. * IsItemClickEnabled == false && IsSwipeEnabled == true 无法响应单击事件,单击和 swipe 均意味着选中
  22. * IsItemClickEnabled == true && IsSwipeEnabled == true 可以响应单击事件,swipe 则意味着选中
  23. */
  24.  
  25. using System.Collections.Generic;
  26. using Windows.UI.Xaml.Controls;
  27. using XamlDemo.Model;
  28.  
  29. namespace XamlDemo.Controls.GridView
  30. {
  31. public sealed partial class Demo : Page
  32. {
  33. public Demo()
  34. {
  35. this.InitializeComponent();
  36.  
  37. // 绑定数据
  38. List<Employee> dataSource = TestData.GetEmployees();
  39. gridView.ItemsSource = dataSource;
  40. }
  41.  
  42. // 单击行为的事件
  43. private void gridView_ItemClick_1(object sender, ItemClickEventArgs e)
  44. {
  45. lblMsg.Text = "被单击的 employee 的 name 为:" + (e.ClickedItem as Employee).Name;
  46. }
  47.  
  48. // 选中行为的事件
  49. private void gridView_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
  50. {
  51. if (e.AddedItems.Count > )
  52. lblMsg.Text = "此次操作被选中的 employee 的 name 为:" + (e.AddedItems[] as Employee).Name;
  53. else
  54. lblMsg.Text = "此次操作没有被选中的 employee";
  55. }
  56. }
  57. }

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. codeforces 659B Qualifying Contest

    题目链接:http://codeforces.com/problemset/problem/659/B 题意: n个人,m个区.给出n个人的姓名(保证不相同),属于的区域,所得分数.从每个区域中选出成 ...

  2. delphi 对抗任务管理器关闭(提升进程到Debug模式,然后设置进程信息SE_PROC_INFO)

    [delphi] view plain copy program Project1; uses Windows; {$R *.res} function MakeMeCritical(Yes: Boo ...

  3. 调用ShellExecute需要头文件

    调用ShellExecute需要头文件 #include   "windows.h " #include   "shellapi.h "

  4. Ajenti 1.0 发布,服务器管理系统 - 开源中国社区

    Ajenti 1.0 发布,服务器管理系统 - 开源中国社区 Ajenti 1.0 发布,服务器管理系统

  5. API - 微云

    API - 微云 1.接口说明 2.数据上传协议说明 1. 接口说明 文件上传申请,成功会返回实际上传的地址. 根据申请上传返回的地址,组织数据上传. 1.1 URL OAuth2.0协议: http ...

  6. Invalid character constant

    Invalid character constant 无效的字符常数 可能是双引号写成了单引号了.

  7. Web Api 2(Cors)Ajax跨域访问

    支持Ajax跨域访问ASP.NET Web Api 2(Cors)的简单示例教程演示   随着深入使用ASP.NET Web Api,我们可能会在项目中考虑将前端的业务分得更细.比如前端项目使用Ang ...

  8. C# 开发Chrome内核浏览器(WebKit.net)

    原文地址:http://www.cnblogs.com/linyijia/p/4045333.html

  9. java+mysql对于表情的处理

    环境错误: mysql 5.0: utf8编码 jdbc:mysql-connector-java-5.1.5-bin.jar 情符的情况下报错: java.sql.SQLException: Inc ...

  10. COCO-Android开发框架公布

    一. COCO-Android说明 二. COCO-Android结构图 三. COCOBuild 四. COCOFrame 一.COCO-Android说明 1. COCO-Android是支撑An ...