原文:重新想象 Windows 8 Store Apps (5) - 控件之集合控件: ComboBox, ListBox, FlipView, ItemsControl, ItemsPresenter

[源码下载]

重新想象 Windows 8 Store Apps (5) - 控件之集合控件: ComboBox, ListBox, FlipView, ItemsControl, ItemsPresenter

作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之集合控件

  • ComboBox - 下拉框
  • ListBox - 列表框
  • FlipView - 滑动视图控件
  • ItemsControl ItemsPresenter - ItemsPresenter 用来呈现 ItemsControl 的 Items

示例
1、ComboBox 的 Demo
ComboBoxDemo.xaml

<Page
x:Class="XamlDemo.Controls.ComboBoxDemo"
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"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <!--ComboBox - 下拉框--> <!--xaml 方式为 ComboBox 添加数据-->
<ComboBox x:Name="comboBox" Width="200" Margin="5" HorizontalAlignment="Left">
<ComboBoxItem Content="ComboBoxItem1" />
<ComboBoxItem Content="ComboBoxItem2" />
<ComboBoxItem Content="ComboBoxItem3" />
</ComboBox> <!--
后台绑定方式为 ComboBox 添加数据
DisplayMemberPath - 指定数据源中需要被显示出来的字段名称
MaxDropDownHeight - 用于指定打开后的下拉框的最大高度
-->
<ComboBox x:Name="comboBoxWithBinding" DisplayMemberPath="Name" MaxDropDownHeight="100" Width="200" Margin="5" HorizontalAlignment="Left" /> <!--通过模板设置 ComboBox 的每一项的布局和数据-->
<ComboBox ItemsSource="{Binding ItemsSource, ElementName=comboBoxWithBinding}" MaxDropDownHeight="100" Width="200" Margin="5" HorizontalAlignment="Left">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding Age}" Margin="5 0 0 0" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox> </StackPanel>
</Grid>
</Page>

ComboBoxDemo.xaml.cs

/*
* ComboBox - 下拉框
* IsDropDownOpen - 下拉框是否处于打开状态
* MaxDropDownHeight - 打开后的下拉框的最大高度
* DropDownOpened - 下拉框打开时触发的事件
* DropDownClosed - 下拉框关闭时触发的事件
*/ using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using XamlDemo.Model; namespace XamlDemo.Controls
{
public sealed partial class ComboBoxDemo : Page
{
public ComboBoxDemo()
{
this.InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 绑定数据到 ComboBox
var data = TestData.GetEmployees();
comboBoxWithBinding.ItemsSource = data;
}
}
}

2、ListBox 的 Demo
ListBoxDemo.xaml

<Page
x:Class="XamlDemo.Controls.ListBoxDemo"
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"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0" Orientation="Horizontal"> <!--ListBox - 列表框--> <!--xaml 方式为 ListBox 添加数据-->
<ListBox x:Name="listBox" Width="200" Height="300" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top">
<ListBox.Items>
<ListBoxItem Content="ListBoxItem1" />
<ListBoxItem Content="ListBoxItem2" />
<ListBoxItem Content="ListBoxItem3" />
</ListBox.Items>
</ListBox> <!--
后台绑定方式为 ListBox 添加数据
DisplayMemberPath - 指定数据源中需要被显示出来的字段名称
-->
<ListBox x:Name="listBoxWithBinding" SelectionMode="Multiple" DisplayMemberPath="Name" Width="200" Height="300" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top" /> <!--通过模板设置 ListBox 的每一项的布局和数据-->
<ListBox ItemsSource="{Binding ItemsSource, ElementName=listBoxWithBinding}" SelectionMode="Multiple" Width="200" Height="300" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding Age}" Margin="5 0 0 0" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<!--
VirtualizingStackPanel - 虚拟化的 StackPanel(即仅生成需要显示的 UI 元素。当绑定了大量数据,而某时仅显示其中一小部分的时候,使用此控件则可大幅提高呈现效率)
Orientation - 数据的排列方式(垂直排列或水平排列,也就是说 ListBox 也可以水平排列)
VirtualizationMode - 虚拟化的模式
Recycling - item 的容器会被重用,默认值
Standard - 每个 item 都有自己独立的容器 注:ListBox 默认已经使用了 VirtualizingStackPanel,但是其对于变高的 DataTemplate 来说支持得不好
-->
<VirtualizingStackPanel Orientation="Vertical" VirtualizingStackPanel.VirtualizationMode="Recycling" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox> </StackPanel>
</Grid>
</Page>

ListBoxDemo.xaml.cs

/*
* ListBox - 列表框
* SelectionMode - 选择的模式(Single - 单选;Multiple - 仅通过鼠标多选;Extended - 通过鼠标和辅助键多选)
* ScrollIntoView(object item) - 滚动到指定 item
* SelectAll() - 选中所有项
*/ using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using XamlDemo.Model; namespace XamlDemo.Controls
{
public sealed partial class ListBoxDemo : Page
{
public ListBoxDemo()
{
this.InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 绑定数据到 ListBox
var data = TestData.GetEmployees();
listBoxWithBinding.ItemsSource = data;
}
}
}

3、FlipView 的 Demo
FlipViewDemo.xaml

<Page
x:Class="XamlDemo.Controls.FlipViewDemo"
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"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <!--FlipView - 滑动视图控件--> <!--在 XAML 中通过 FlipViewItem 指定 FilpView 的每一项-->
<FlipView Width="320" Height="240" HorizontalAlignment="Left" Background="Black">
<FlipView.Items>
<FlipViewItem>
<TextBlock FontSize="26.667" Text="I am webabcd" />
</FlipViewItem>
<FlipViewItem>
<Image Source="/Assets/Logo.png" Stretch="Fill" />
</FlipViewItem>
</FlipView.Items>
</FlipView> <!--通过后台绑定的方式将数据绑定到 FlipView-->
<FlipView Name="flipView" Width="320" Height="240" Background="Yellow" HorizontalAlignment="Left" Margin="0 10 0 0">
<FlipView.ItemContainerStyle>
<Style TargetType="FlipViewItem">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>
</FlipView.ItemContainerStyle>
<!--上下翻页-->
<FlipView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</FlipView.ItemsPanel>
<FlipView.ItemTemplate>
<DataTemplate>
<Grid Background="Black">
<TextBlock Text="{Binding Name}" FontSize="26.667" />
<TextBlock Text="{Binding Age}" FontSize="26.667" Margin="0 40 0 0" />
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView> </StackPanel>
</Grid>
</Page>

FlipViewDemo.xaml.cs

/*
* FlipView - 滑动视图控件
*/ using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using XamlDemo.Model; namespace XamlDemo.Controls
{
public sealed partial class FlipViewDemo : Page
{
public FlipViewDemo()
{
this.InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 绑定数据到 FlipView
var employees = TestData.GetEmployees();
flipView.ItemsSource = employees;
}
}
}

4、ItemsControl, ItemsPresenter 的 Demo
ItemsControlDemo.xaml

<Page
x:Class="XamlDemo.Controls.ItemsPresenterDemo"
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"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <!--
演示 ItemsControl 和 ItemsPresenter 的应用(ItemsPresenter 用来呈现 ItemsControl 的 Items) ListBox, ComboBox, FlipView, GridView, ListView 等均间接地继承了 ItemsControl
-->
<ItemsControl HorizontalAlignment="Left">
<ItemsControl.Items>
<Rectangle Width="100" Height="100" Fill="Red" />
<Rectangle Width="100" Height="100" Fill="Green" />
<Rectangle Width="100" Height="100" Fill="Blue" />
</ItemsControl.Items>
<ItemsControl.Template>
<ControlTemplate>
<Border BorderBrush="Orange" BorderThickness="1" Width="400" Height="400">
<!--
通过 ItemsPresenter 来呈现 ItemsControl 的 Items(注:其呈现的是 Items 而不是 Item)
-->
<ItemsPresenter Margin="10" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Border>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl> </StackPanel>
</Grid>
</Page>

OK

[源码下载]

重新想象 Windows 8 Store Apps (5) - 控件之集合控件: ComboBox, ListBox, FlipView, ItemsControl, ItemsPresenter的更多相关文章

  1. 重新想象 Windows 8 Store Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  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 (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding

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

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

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

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

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

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

    原文:重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView [源码下载] 重新想象 Windows 8 Store Apps (11) - ...

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

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

随机推荐

  1. 如何隐藏 QLPreviewController 的 Action 按钮?

    在 iOS 6 以前,可以在 present QLPreviewController 之后使用以下代码: [previewController.navigationItem setRightBarBu ...

  2. C#验证字符串是否是数字,是否包括中文,是否是邮箱格式,是否是电话格式

    using System;     using System.Web;     using System.Text;     using System.Web.UI.WebControls;     ...

  3. http staus汇总

    参考http://www.cnblogs.com/cxd4321/archive/2008/11/20/1337776.html 常见HTTP状态码 200 OK 301 Moved Permanen ...

  4. [cocos2d-x][apk打包][Fatal signal 11][andriod]Eclipse编译Fatal signal 11报错-都是字符赋值惹的祸

    流程重现: 使用coco2d-x制作了一个2048,在xcode模拟器执行以及在pad上真机调试都是没有问题的. 可是在使用eclipse调试打包android可以执行,可是进入游戏之后会在随机的地方 ...

  5. UVa 10616 - Divisible Group Sums

    称号:给你n数字.免去m一个,这使得他们可分割d.问:有多少种借贷. 分析:dp,D01背包. 背包整数分区. 首先.整点d.则全部数字均在整数区间[0,d)上: 然后,确定背包容量,最大为20*10 ...

  6. csdn android视频播放器开发

    http://blog.csdn.net/column/details/myvideo.html

  7. 【Struts2学习笔记(11)】对action的输入校验和XML配置方式实现对action的全部方法进行输入校验

    在struts2中,我们能够实现对action的全部方法进行校验或者对action的指定方法进行校验. 对于输入校验struts2提供了两种实现方法: 1. 採用手工编写代码实现. 2. 基于XML配 ...

  8. Android开发 更改返回button的图标

    非常多的Android应用左上角都有返回button 在默认的情况下 ADT会默认给一个返回图标 而作为开发需求 非常多都要求定制一个新的图标 在Android的站点上 发现了2种能够更改的方法 1. ...

  9. HADOOP2.6

    LINUX下HADOOP2.6.0集群环境的搭建 本文旨在提供最基本的,可以用于在生产环境进行Hadoop.HDFS分布式环境的搭建,对自己是个总结和整理,也能方便新人学习使用. 基础环境 JDK的安 ...

  10. 基于CefGlue的桌面应用开发

    原文地址:http://johnnyfee.github.io/csharp/2013/12/21/cef-glue/ 前言 如果你想使用WEB技术来开发桌面客户端,并且是想使用的语言也是C#时,那请 ...