`
dyllove98
  • 浏览: 1405415 次
  • 性别: Icon_minigender_1
  • 来自: 济南
博客专栏
73a48ce3-d397-3b94-9f5d-49eb2ab017ab
Eclipse Rcp/R...
浏览量:39058
4322ac12-0ba9-3ac3-a3cf-b2f587fdfd3f
项目管理checkList...
浏览量:80060
4fb6ad91-52a6-307a-9e4f-816b4a7ce416
哲理故事与管理之道
浏览量:133167
社区版块
存档分类
最新评论

重新想象 Windows 8 Store Apps (11)

 
阅读更多

[源码下载]


重新想象 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 > 0)
                lblMsg.Text = "此次操作被选中的 employee 的 name 为:" + (e.AddedItems[0] 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 > 0)
                lblMsg.Text = "此次操作被选中的 employee 的 name 为:" + (e.AddedItems[0] as Employee).Name;
            else
                lblMsg.Text = "此次操作没有被选中的 employee";
        }
    }
}



OK
[源码下载]

分享到:
评论

相关推荐

    Windows 8 Store Apps源码20130912

    重新想象Windows 8 Store Apps中包含了windows8 App demo,包括Control(控件)、布局控件、WebView、ScrollViewer、LV GV SZ控件、控件UI 控件基础、绘图、动画、文件系统、Picker(选取器)、图片处理、信息、等...

    Programming Windows Store Apps with C#

    If you’re a .NET developer looking to build tablet apps, this practical book takes you step-by-step through the process of developing apps for the Windows Store. You’ll learn how to use Microsoft’s...

    Windows 8 store style apps development resources document

    根据给定的文件信息,以下是对Windows 8 Store风格应用开发资源文档的详细解析与相关知识点的阐述: ### Windows 8 Store风格应用开发资源 #### 概述 本文档主要探讨了在Windows 8和Windows RT平台上开发Store...

    Designing for Windows 8_Fundamentals of Great Design in Windows Store Apps

    在《为Windows 8设计:Windows Store应用中的优秀设计原则》这一著作中,作者Brent Schooley深入探讨了创建出色Windows 8应用的关键要素。本书作为Win8 Store App程序设计的基础指南,通过五章节的内容,提供了全面...

    Programming.Windows.Writing.Windows.8.Apps.With.C#.and.XAML.6th.edition.2013

    Reimagined for full-screen and touch-optimized apps, Windows 8 provides a platform for reaching new users in new ways. In response, programming legend Charles Petzold is rewriting his classic ...

    Bing Maps SDK for Windows Store apps 扩展.zip

    必应地图 SDK扩展整合了 Windows 8.1 和必应地图的强大功能,以将最新的道路...注:如果操作系统是Windows 8 Release Preview,需要安装对应的预览版用SDK Bing Maps SDK for Windows Store apps Bing Maps SDK截图:

    Windows Store App Development: C# and XAML

    The Windows Store provides an amazing array of productivity tools, games, and other apps directly to the millions of customers already using Windows 8.x or Surface. Windows Store apps boast new ...

    Essentials of Developing Windows Store Apps Using C#

    Essentials of Developing Windows Store Apps Using C# With universal Windows apps, it’s finally possible to create an app that targets both PCs (desktops, laptops, tablets, and hybrids) and phones ...

    Exam Ref 70-484 Essentials of Developing Windows Store Apps Using C#

    ### Exam Ref 70-484 Essentials of Developing Windows Store Apps Using C# #### 知识点一:考试目标与认证背景 - **考试编号**:70-484 - **考试名称**:Essentials of Developing Windows Store Apps Using C#...

    Programming.Windows.Writing.Windows.8.Apps.With.C#.and.XAML.6th.edition

    《Programming.Windows.Writing.Windows.8.Apps.With.C#.and.XAML.6th.edition》这本书是Windows 8应用开发的权威指南,专为使用C#和XAML进行编程的开发者设计。第六版针对Windows 8操作系统提供了最新的技术和实践...

    使用HTML,CSS和JavaScript对Windows Store应用程序进行编程Programming Windows Store Apps with HTML, CSS, and JavaScript

    这本书是关于在Windows 8.1平台上使用HTML,CSS3和JavaScript编写Windows应用商店应用程序的深入电子书。 该电子书包含20章和4个附录。 我们很高兴欢迎您进入重新构想的Windows世界!

    WindowsStore_11809.zip

    windows应用商店恢复包 安装指引 LTSB 2016 / LTSC 2019应用商店安装包_11809 HWH0488 20181008 1、运行App_Online安装应用商店 2、微软应用安装:在应用商店直接搜索MSN天气、Windows计算器安装即可 安装...

    windows ltsc版本没有Microsoft Store怎么解决

    Add-AppxPackage -DisableDevelopmentMode -Register "C:\Program Files\WindowsApps\Microsoft.WindowsStore_11912.1001.7.0_x64__8wekyb3d8bbwe\AppxManifest.xml" -Verbose ``` 这个命令会尝试重新注册已存在...

    Windows Store App Development

    Windows Store App Development introduces C# developers to working with Windows Store apps. It provides full coverage of XAML, and addresses both app design and development. Following numerous ...

    C#Windows store 音乐播放器

    2. **Windows Store应用**:Windows Store应用(也称为Modern UI或Universal App)是专为Windows 8及以上操作系统设计的触控优先的应用。它们在Windows应用商店中发布,遵循特定的设计原则和API,以提供一致且现代的...

    Exam Ref 70-484: Essentials of Developing Windows Store Apps using C#

    从文件内容中提取知识点,首先需要注意的是,此文件是一本针对准备微软认证考试70-484的参考书,名为《Exam Ref 70-484: Essentials of Developing Windows Store Apps using C#》(考试参考:70-484 Windows商店...

Global site tag (gtag.js) - Google Analytics