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的更多相关文章

  1. 背水一战 Windows 10 (77) - 控件(控件基类): ContentControl, UserControl, Page

    [源码下载] 背水一战 Windows 10 (77) - 控件(控件基类): ContentControl, UserControl, Page 作者:webabcd 介绍背水一战 Windows ...

  2. 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中的元素

    [源码下载] 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中 ...

  3. 背水一战 Windows 10 (75) - 控件(控件基类): FrameworkElement - 基础知识, 相关事件, HorizontalAlignment, VerticalAlignment

    [源码下载] 背水一战 Windows 10 (75) - 控件(控件基类): FrameworkElement - 基础知识, 相关事件, HorizontalAlignment, Vertical ...

  4. VS2010 使用TeeChart绘图控件 - 之一 - 控件和类的导入

    vs2010的用法和vc6有很大的不同,特别是在一些函数调用那里,当然.控件导入也是很不一样的 安装好控件后就可以在工程里加入teechart控件了 加入方法有如下几种: 1.添加Teechart控件 ...

  5. File控件选择图片的时候在Html5下马上预览

    页面HTML <div> <img src="@pic.Path" id="img" style="width:200px;heig ...

  6. ExtJs内的datefield控件选择日期过后的事件监听select

    [摘要]: 选择时间过后我们为什么需要监听事件?一般有这样一种情况,那就是用于比较两个时间大小或者需要判断在哪个时间点上需要做什么样的操作.基于这样的种种情况,我们很有必要琢磨一下datefield控 ...

  7. asp.net 弹出式日历控件 选择日期 Calendar控件

    原文地址:asp.net 弹出式日历控件 选择日期 Calendar控件 作者:逸苡 html代码: <%@ Page Language="C#" CodeFile=&quo ...

  8. 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件

    [源码下载] 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件 作者:webabcd 介绍 ...

  9. 背水一战 Windows 10 (73) - 控件(控件基类): UIElement - 拖放的基本应用, 手动开启 UIElement 的拖放操作

    [源码下载] 背水一战 Windows 10 (73) - 控件(控件基类): UIElement - 拖放的基本应用, 手动开启 UIElement 的拖放操作 作者:webabcd 介绍背水一战 ...

  10. 背水一战 Windows 10 (72) - 控件(控件基类): UIElement - UIElement 的位置, UIElement 的布局, UIElement 的其他特性

    [源码下载] 背水一战 Windows 10 (72) - 控件(控件基类): UIElement - UIElement 的位置, UIElement 的布局, UIElement 的其他特性 作者 ...

随机推荐

  1. [No00005A]word多文档合一

    2个方法:法一,一个个插入,法二,一次性插入多个. 法一: 视图->大纲视图 点击 大纲 -> 显示文档 点击插入,逐个插入文档.. 最终 将视图调回页面视图..结束. 法二: 插入 - ...

  2. 转:eclipse的workspace和working set

    from: http://iyuanbo.iteye.com/blog/1158136   eclipse的workspace和working set 2015-05-20 09:28:48 标签:e ...

  3. SQL注入技术专题—由浅入深【精华聚合】

    作者:坏蛋链接:https://zhuanlan.zhihu.com/p/23569276来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 不管用什么语言编写的Web应用 ...

  4. HTML 学习笔记 CSS(选择器3)

    CSS 属性选择器 属性选择器可以根据元素的额属性以及属性值来选择元素 例子1 如果 你希望把包含title的所有元素变成红色 *[title] {color:red} 例子2 与上面类似 可以只对有 ...

  5. JS中new都是干了些什么事情

    var Person = function(name){ this.name = name; this.say = function(){ return "I am " + thi ...

  6. Java核心技术点之集合框架

    1. 概述     Java集合框架由Java类库的一系列接口.抽象类以及具体实现类组成.我们这里所说的集合就是把一组对象组织到一起,然后再根据不同的需求操纵这些数据.集合类型就是容纳这些对象的一个容 ...

  7. 解决ssh-connect-to-host-github-com-port-22-connection-timed-out

    PC:~$ ssh git@github.com ssh: connect to host github.com port 22: Connection timed out 解决办法:(linux下) ...

  8. 【WPF】TextBox样式重写注意事项

    1.普通控件重写需要添加一行 <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="C ...

  9. ios开发--多台电脑共用一个开发证书的方法

    Xcode5 以前的操作步骤是: idp证书如何给另一台机子使用 先在原电脑上用Xcode->Windows->Organizer, 再点击Developer profile, 在其最下面 ...

  10. QT QToolBox类

    QToolBox类的创建 //drawer.h #ifndef DRAWER_H #define DRAWER_H #include <QToolBox> #include <QTo ...