控件(选择类): Selector, ComboBox
1、Selector(基类) 的示例
Controls/SelectionControl/SelectorDemo.xaml

<Page
x:Class="Windows10.Controls.SelectionControl.SelectorDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.SelectionControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" xmlns:common="using:Windows10.Common"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <TextBlock Name="textBlock" Margin="5" /> <!--
ComboBox - 下拉框控件,继承自 Selector,下面介绍 Selector 的相关知识点
-->
<ComboBox Name="comboBox1" Margin="5 20 5 5" ItemsSource="{x:Bind Employees}" Width="200" HorizontalAlignment="Left">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="common:Employee">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{x:Bind Name}" />
<TextBlock Text="{x:Bind Age}" Margin="5 0 0 0" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock Name="lblMsg1" Margin="5" /> <ComboBox Name="comboBox2" Margin="5 20 5 5" ItemsSource="{x:Bind Employees}" Width="200" HorizontalAlignment="Left">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="common:Employee">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{x:Bind Name}" />
<TextBlock Text="{x:Bind Age}" Margin="5 0 0 0" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock Name="lblMsg2" Margin="5" /> <!--
ComboBoxItem - 下拉框控件的 item,继承自 SelectorItem,下面介绍 SelectorItem 的相关知识点
IsSelected - 是否被选中
-->
<ComboBox x:Name="comboBox3" Margin="5 20 5 5" Width="200" HorizontalAlignment="Left">
<ComboBoxItem Content="ComboBoxItem1" IsSelected="True" />
<ComboBoxItem Content="ComboBoxItem2" />
<ComboBoxItem Content="ComboBoxItem3" />
</ComboBox> </StackPanel>
</Grid>
</Page>

Controls/SelectionControl/SelectorDemo.xaml.cs

/*
* Selector(基类) - 选择器控件基类(继承自 ItemsControl, 请参见 /Controls/CollectionControl/ItemsControlDemo/)
* SelectedIndex - 选中项的索引
* SelectedItem - 选中项的数据对象
* SelectedValuePath - 选中项的值的字段路径,默认值为空字符串(此时 SelectedValue 的结果与 SelectedItem 相同)
* SelectedValue - 选中项的值(字段路径通过 SelectedValuePath 设置)
* bool GetIsSelectionActive(DependencyObject element) - 用于获取指定的 Selector 控件是否是焦点状态
* 如果是焦点状态,则按下键盘 enter 键会弹出此 Selector 控件的选项列表,按下 esc 键会隐藏此 Selector 控件的选项列表
* IsSynchronizedWithCurrentItem - 暂时认为没用吧,因为设置为 true 后,在 runtime 会抛出 Windows.UI.Xaml.Markup.XamlParseException 异常
* SelectionChanged - 选中项发生变化时触发的事件
*
*
* SelectorItem(基类) - Selector 的 Item(继承自 ContentControl, 请参见 /Controls/BaseControl/ContentControlDemo/)
* IsSelected - 是否被选中
*/ using System;
using System.Collections.ObjectModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows10.Common; namespace Windows10.Controls.SelectionControl
{
public sealed partial class SelectorDemo : Page
{
public ObservableCollection<Employee> Employees { get; set; } = TestData.GetEmployees(30); public SelectorDemo()
{
this.InitializeComponent(); this.Loaded += SelectorDemo_Loaded; // 不设置 SelectedValuePath,则 SelectedValue 的结果与 SelectedItem 相同
comboBox1.SelectedValuePath = "";
comboBox1.SelectionChanged += ComboBox1_SelectionChanged; // 指定 SelectedValue 的字段路径
comboBox2.SelectedValuePath = "Name";
comboBox2.SelectionChanged += ComboBox2_SelectionChanged;
} private void SelectorDemo_Loaded(object sender, RoutedEventArgs e)
{
DispatcherTimer dTimer = new DispatcherTimer();
dTimer.Interval = TimeSpan.Zero;
dTimer.Tick += DTimer_Tick;
dTimer.Start();
} private void DTimer_Tick(object sender, object e)
{
textBlock.Text = $"comboBox1 focus:{ComboBox.GetIsSelectionActive(comboBox1)}, comboBox2 focus:{ComboBox.GetIsSelectionActive(comboBox2)}";
} private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// e.RemovedItems - 本次事件中,被取消选中的项
// e.AddedItems - 本次事件中,新被选中的项 int selectedIndex = comboBox1.SelectedIndex; // SelectedItem 是选中的 Employee 对象
// SelectedValue 是选中的 Employee 对象
lblMsg1.Text = $"comboBox1 SelectedItem:{comboBox1.SelectedItem}, SelectedValue:{comboBox1.SelectedValue}";
} private void ComboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selectedIndex = comboBox2.SelectedIndex; // SelectedItem 是选中的 Employee 对象
// SelectedValue 是选中的 Employee 对象的 Name 属性的值
lblMsg2.Text = $"comboBox2 SelectedItem:{comboBox2.SelectedItem}, SelectedValue:{comboBox2.SelectedValue}";
}
}
}

2、ComboBox 的示例
Controls/SelectionControl/ComboBoxDemo.xaml

<Page
x:Class="Windows10.Controls.SelectionControl.ComboBoxDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.SelectionControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" xmlns:common="using:Windows10.Common"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <!--
ComboBox - 下拉框控件
Header - 可以设置一个纯文本,不能命中测试,空 Header 的话不会占用任何空间
HeaderTemplate - 可以将 Header 设置为任何 xaml,且支持命中测试
PlaceholderText - 占位符水印
--> <!--通过 xaml 方式为 ComboBox 添加数据-->
<ComboBox x:Name="comboBox1" Margin="5" Width="200" HorizontalAlignment="Left"
Header="comboBox1" PlaceholderText="PlaceholderText">
<ComboBoxItem Content="ComboBoxItem1" />
<ComboBoxItem Content="ComboBoxItem2" />
<ComboBoxItem Content="ComboBoxItem3" />
</ComboBox>
<TextBlock Name="lblMsg1" Margin="5" /> <!--为 ComboBox 绑定数据-->
<ComboBox x:Name="comboBox2" ItemsSource="{x:Bind Employees}" Margin="5 20 5 5" Width="200" HorizontalAlignment="Left">
<ComboBox.HeaderTemplate>
<DataTemplate>
<TextBlock Text="comboBox2" Foreground="Red" />
</DataTemplate>
</ComboBox.HeaderTemplate>
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="common:Employee">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{x:Bind Name}" />
<TextBlock Text="{x:Bind Age}" Margin="5 0 0 0" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox> <!--通过 xaml 方式为 ComboBox 添加数据(直接用字符串的方式),在 code-behind 中可以通过 SelectedValue 直接获取选中的字符串-->
<ComboBox Name="comboBox3" SelectedIndex="0" Width="200" HorizontalAlignment="Left" Margin="5 60 5 5">
<x:String>Red</x:String>
<x:String>Green</x:String>
<x:String>Blue</x:String>
</ComboBox> </StackPanel>
</Grid>
</Page>

Controls/SelectionControl/ComboBoxDemo.xaml.cs

/*
* ComboBox - 下拉框控件(继承自 Selector, 请参见 /Controls/SelectionControl/SelectorDemo.xaml)
* DropDownOpened - 下拉框打开(弹出选项列表)时触发的事件
* DropDownClosed - 下拉框关闭(隐藏选项列表)时触发的事件
* IsDropDownOpen - 下拉框是否处于打开状态
* MaxDropDownHeight - 下拉框打开后,其选项列表的最大高度
* SelectionBoxItem - 下拉框关闭后显示的数据对象(即下拉框的选项列表隐藏后,在下拉框中显示的数据对象
*
*
* ComboBoxItem - 下拉框控件的 item(继承自 SelectorItem, 请参见 /Controls/SelectionControl/SelectorDemo.xaml)
*/ using System.Collections.ObjectModel;
using Windows.UI.Xaml.Controls;
using Windows10.Common; namespace Windows10.Controls.SelectionControl
{
public sealed partial class ComboBoxDemo : Page
{
public ObservableCollection<Employee> Employees { get; set; } = TestData.GetEmployees(30); public ComboBoxDemo()
{
this.InitializeComponent(); comboBox1.DropDownOpened += ComboBox1_DropDownOpened;
comboBox1.DropDownClosed += ComboBox1_DropDownClosed; comboBox2.MaxDropDownHeight = 40;
comboBox2.Loaded += (x, y) =>
{
// 注:如果要设置 IsDropDownOpen 属性的话,需要等到 ComboBox 加载后在设置
comboBox2.IsDropDownOpen = true;
};
} private void ComboBox1_DropDownOpened(object sender, object e)
{
lblMsg1.Text = "comboBox1 DropDownOpened";
} private void ComboBox1_DropDownClosed(object sender, object e)
{
// 通过 SelectionBoxItem 可获取 ComboBox 的选项列表隐藏后,在 ComboBox 中显示的数据对象
lblMsg1.Text = $"comboBox1 DropDownClosed, SelectionBoxItem:{comboBox1.SelectionBoxItem}";
}
}
}
控件(选择类): Selector, ComboBox的更多相关文章
- 背水一战 Windows 10 (77) - 控件(控件基类): ContentControl, UserControl, Page
[源码下载] 背水一战 Windows 10 (77) - 控件(控件基类): ContentControl, UserControl, Page 作者:webabcd 介绍背水一战 Windows ...
- 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中的元素
[源码下载] 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中 ...
- 背水一战 Windows 10 (75) - 控件(控件基类): FrameworkElement - 基础知识, 相关事件, HorizontalAlignment, VerticalAlignment
[源码下载] 背水一战 Windows 10 (75) - 控件(控件基类): FrameworkElement - 基础知识, 相关事件, HorizontalAlignment, Vertical ...
- VS2010 使用TeeChart绘图控件 - 之一 - 控件和类的导入
vs2010的用法和vc6有很大的不同,特别是在一些函数调用那里,当然.控件导入也是很不一样的 安装好控件后就可以在工程里加入teechart控件了 加入方法有如下几种: 1.添加Teechart控件 ...
- File控件选择图片的时候在Html5下马上预览
页面HTML <div> <img src="@pic.Path" id="img" style="width:200px;heig ...
- ExtJs内的datefield控件选择日期过后的事件监听select
[摘要]: 选择时间过后我们为什么需要监听事件?一般有这样一种情况,那就是用于比较两个时间大小或者需要判断在哪个时间点上需要做什么样的操作.基于这样的种种情况,我们很有必要琢磨一下datefield控 ...
- asp.net 弹出式日历控件 选择日期 Calendar控件
原文地址:asp.net 弹出式日历控件 选择日期 Calendar控件 作者:逸苡 html代码: <%@ Page Language="C#" CodeFile=&quo ...
- 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件
[源码下载] 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件 作者:webabcd 介绍 ...
- 背水一战 Windows 10 (73) - 控件(控件基类): UIElement - 拖放的基本应用, 手动开启 UIElement 的拖放操作
[源码下载] 背水一战 Windows 10 (73) - 控件(控件基类): UIElement - 拖放的基本应用, 手动开启 UIElement 的拖放操作 作者:webabcd 介绍背水一战 ...
- 背水一战 Windows 10 (72) - 控件(控件基类): UIElement - UIElement 的位置, UIElement 的布局, UIElement 的其他特性
[源码下载] 背水一战 Windows 10 (72) - 控件(控件基类): UIElement - UIElement 的位置, UIElement 的布局, UIElement 的其他特性 作者 ...
随机推荐
- Makefile总述②文件命名、包含其他文件makefile、变量、重建重载、解析
Makefile的内容 在一个完整的 Makefile 中,包含了 5 个东西:显式规则.隐含规则.变量定义.指示符和注释. 显式规则:它描述了在何种情况下如何更新一个或者多个被称为目标的文件( Ma ...
- win10的安装与下载
1.下载介质创建工具 https://www.microsoft.com/zh-cn/software-download/windows10 2. 下载iso https://www.microsof ...
- Zygote进程【3】——SystemServer的诞生
在ZygoteInit的main()方法中做了几件大事,其中一件便是启动Systemserver进程,代码如下: @/frameworks/base/core/Java/com/Android/int ...
- Android开发书籍推荐:从入门到精通系列学习路线书籍介绍
Android开发书籍推荐:从入门到精通系列学习路线书籍介绍 很多时候我们都会不断收到新手的提问"Android开发的经典入门教材和学习路线?"."Android 开发入 ...
- SPM - data analysis
来源: SPM基本原理与使用PPT, 北师大,朱朝喆研究员,http://www.cnblogs.com/haore147/p/3633515.html ❤ First-level analysis: ...
- Mysql导出函数、存储过程
下面是导出存储过程的代码 1 # mysqldump -u 数据库用户名 -p -n -t -d -R 数据库名 > 文件名 其中,-d 表示--no-create-db, -n表示--no-d ...
- workqueue机制分析之process_one_work分析
工作者线程不断执行,从work_poll结构中卸下一个work, 然后进入函数process_one_work 来执行这个work. process_one_work(struct worker *w ...
- keytool命令记录
1.生成服务器端私钥kserver.keystore文件 2.根据私钥,导出服务器端安全证书 3.将服务器端证书,导入到客户端的Trust KeyStore中 4.生成客户端私钥kclient.key ...
- 招聘高级.Net工程师
找钢网创新开发部真诚地邀请程序猿\媛们加入,一起来吃大闸蟹午餐. 在创新开发部你可以见证一个产品从零开始到爆发到改变一个大宗商品的行业,在创新开发部你有机会接触到国际范,你还有机会接触到韩国的妹纸.欧 ...
- C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本 - 用户权限树的实现 -- 权限递归树
业务系统里经常会需要计算类似的树形权限树的业务需求 1:往往会有一些需求,a 对 b 有权限, b对c 有权限, 等等. 2:还需要很直观的看到,整个权限的树形关系,一目了然的那种. 3:程序调用简单 ...