在 UWP 中实现 Expander 控件
WPF 中的 Expander 控件在 Windows 10 SDK 中并不提供,本文主要说明,如何在 UWP 中创建这样一个控件。其效果如下图:

首先,分析该控件需要的一些特性,它应该至少包括如下三个属性:
- Content: 最重要的属性,设置该属性,可以使 Expander 控件显示其内容;
- Header: 控件的 Header;
- IsExpand: 当前是否展开。
接下来是定义其 UI,在这里使用 Grid,添加两行,一行显示 Header,一行显示 Content,当 IsExpand 属性为 false 时,只要将 Content 那一行隐藏即可;此外,还需要一个 ToggleButton 用于控制该控件的展开与关闭。
OK。思路弄清楚后,开始实践,
1. 创建控件,并添加属性
在项目中添加一个 Templated Control(模板化控件),名称为 Expander。为其添加三个依赖属性,代码如下:
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register("Content", typeof(object), typeof(Expander), new PropertyMetadata(null)); public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register("Header", typeof(object), typeof(Expander), new PropertyMetadata(null)); public static readonly DependencyProperty IsExpandProperty =
DependencyProperty.Register("IsExpand", typeof(bool), typeof(Expander), new PropertyMetadata(true)); /// <summary>
/// 控件的内容
/// </summary>
public object Content
{
get { return (object)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
} /// <summary>
/// 控件的标题
/// </summary>
public object Header
{
get { return (object)GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
} /// <summary>
/// 返回或设置控件是否展开
/// </summary>
public bool IsExpand
{
get { return (bool)GetValue(IsExpandProperty); }
set { SetValue(IsExpandProperty, value); }
}
2. 定义UI
在 Generic.xaml 中,找到 <Style TargetType="controls:Expander"> 节点,添加如下代码:
<Style TargetType="controls:Expander">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:Expander">
<Grid x:Name="grid"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<ToggleButton x:Name="toggleButton"
Width="32"
Height="32"
Margin="0,0,4,0"
BorderThickness="0"
IsChecked="{Binding IsExpand,
RelativeSource={RelativeSource Mode=TemplatedParent},
Mode=TwoWay}">
<Path x:Name="arrow"
Width="16"
Height="16"
Data="M15.289001,0L20.484007,0 31.650999,15.953003 29.055021,19.658005 20.415007,32 15.35501,32 15.289001,31.906998 24.621,18.572998 0,18.572998 0,13.326004 24.621,13.326004z"
Fill="#DDFFFFFF"
RenderTransformOrigin="0.5,0.5"
Stretch="Uniform">
</Path>
</ToggleButton>
<ContentPresenter VerticalAlignment="Center" Content="{TemplateBinding Header}" />
</StackPanel>
<ContentPresenter Grid.Row="1"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
Content="{TemplateBinding Content}"
Visibility="{Binding IsExpand,
RelativeSource={RelativeSource Mode=TemplatedParent},
Converter={StaticResource BooleanToVisibilityConverter},
Mode=TwoWay}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
可以看出:
a) ToggleButton 的 IsChecked 属性绑定了控件的 IsExpand 属性, 绑定表达式 {Binding IsExpand,RelativeSource={RelativeSource Mode=TemplatedParent}} 是 {TemplateBinding IsExpand} 的另一种写法,在这种写法中,我们可以添加 Binding 对象的其它属性,如这里的 Mode=TwoWay,这样可以实现 ToggleButton 与 控件的 IsExpand 属性彼此互相的控制;
b) ContentControl 的 Visibility 与 a) 同理,略微复杂的是,这里用了一个 Converter,用于在 Bool 和 Visibility 枚举之间转换;
c) 我们为 ToggleButton 控件的 Content 属性设置了一个 Path 用来形象地表达 Expander 当前的状态。
3. 定义 VisualState
我们为该控件定义两个 VisualState,分别代表正常状态和展开状态,即 Normal 与 Expanded,通过切换这两种状态可以完成该控件的UI变化,这里主要是对 ToggleButton 的 Content 进行动画设置。
在 Path 中为其添加 RotateTransform,代码如下:
<Path x:Name="arrow"
Width="16"
Height="16"
Data="M15.289001,0L20.484007,0 31.650999,15.953003 29.055021,19.658005 20.415007,32 15.35501,32 15.289001,31.906998 24.621,18.572998 0,18.572998 0,13.326004 24.621,13.326004z"
Fill="#DDFFFFFF"
RenderTransformOrigin="0.5,0.5"
Stretch="Uniform">
<Path.RenderTransform>
<RotateTransform x:Name="pathRotate" />
</Path.RenderTransform>
</Path>
在 Grid 中添加 VisualState,代码如下:
<Grid x:Name="grid"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition From="Normal"
GeneratedDuration="0:0:0.2"
To="Expanded" />
<VisualTransition From="Expanded"
GeneratedDuration="0:0:0.2"
To="Normal" />
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal">
<Storyboard>
<DoubleAnimation Duration="0:0:0"
Storyboard.TargetName="pathRotate"
Storyboard.TargetProperty="Angle"
To="0">
<DoubleAnimation.EasingFunction>
<QuinticEase EasingMode="EaseOut" />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</VisualState>
<VisualState x:Name="Expanded">
<Storyboard>
<DoubleAnimation Duration="0:0:0"
Storyboard.TargetName="pathRotate"
Storyboard.TargetProperty="Angle"
To="90">
<DoubleAnimation.EasingFunction>
<QuinticEase EasingMode="EaseIn" />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
...
这里,我们可以看到,除了两个 VisualState 外,我们还定义了两个 VisualTransition,用来设置切换此两种状态时的过度时间。
提示:关于 Content 区域的隐藏与显示,也可以通过在 VisualState 添加动画来控制,不过在上面的代码中,我们利用了 ToggleButton 以及它的 IsCheced 属性来控制其显示与隐藏,较为简洁地了实现这一功能。
接下来,我们需要在代码中来控制何时在这两种状态间切换,在 Expander.cs 中添加如下代码:
private ToggleButton button;
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
button = GetTemplateChild("toggleButton") as ToggleButton;
button.Loaded += (s, e) => { ChangeControlState(false); };
button.Checked += (s, e) => { ChangeControlState(); };
button.Unchecked += (s, e) => { ChangeControlState(); };
}
/// <summary>
/// 改变控件的 VisualState
/// </summary>
/// <param name="useTransition">是否使用 VisualTransition,默认使用</param>
private void ChangeControlState(bool useTransition = true)
{
if (button.IsChecked.Value)
{
VisualStateManager.GoToState(this, "Expanded", useTransition);
}
else
{
VisualStateManager.GoToState(this, "Normal", useTransition);
}
}
可以看出,我们为 ToggleButton 添加事件响应来切换状态。之所以在 Load 时也来改检查并更改状态,是因为,如果在使 Expander 控件时,如果为它设置 IsExpand 为 true 时,那么加载时,会及时更新控件状态为 Expanded ,否则将默认为 Normal。
最后,我们为控件添加一个 ContentPropertyAttribute,并设置其 Name 为 Content,这样,该控件的 Content 属性就作为此控件的内容属性(ContentPropery)。简言之,可以省去 <xxx:Expander.Content> 这个节点,类似在 Button 中直接添加其 Content 一样。代码如下:
[ContentProperty(Name = "Content")]
public sealed class Expander : Control
至此,一个 Expander 控件就完成了,至于你还有额外、其它的需求(如样式的修改等),则可在此基础上进行修改。
如果你有什么更好的建议或其它观点,请留言,互相交流。
参考资料:
What's the difference between ContentControl and ContentPresenter?
Difference between ContentControl, ContentPresenter, ContentTemplate and ControlTemplate? (Bob_Bao's answer)
What is ContentPropertyAttribute?
在 UWP 中实现 Expander 控件的更多相关文章
- WPF中Expander控件样式,ListBox的样式(带checkbox)恢复
Expander控件样式: <ControlTemplate x:Key="ExpanderToggleButton" TargetType="ToggleButt ...
- Win10 UWP开发系列——开源控件库:UWPCommunityToolkit
在开发应用的过程中,不可避免的会使用第三方类库.之前用过一个WinRTXamlToolkit.UWP,现在微软官方发布了一个新的开源控件库—— UWPCommunityToolkit 项目代码托管在G ...
- WPF Expander控件(扩展面板)
这算是我比较喜欢的一个控件,以前在Winform中也常用类似的.它包装了一块内容,通过单击一个小箭头按钮可以显示或隐藏所包含的内容.在线帮助以及Web页面经常使用这种技术,因为既可以包含大量内容,而又 ...
- 《WPF》Expander控件简单美化
示例图: Expander控件功能很常见, 一般用于系统左侧的菜单收缩面板. 1.主要的组成 一个头部(header) 和 一个 内容(content) 组成. <Expander Expand ...
- 在DevExpress程序中使用SplashScreenManager控件实现启动闪屏和等待信息窗口
在我很早的WInform随笔<WinForm界面开发之"SplashScreen控件">有介绍如何使用闪屏的处理操作,不过那种是普通WInform和DevExpress ...
- 在WPF中使用WinForm控件方法
1. 首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll. 2. 在要使用WinForm控 ...
- wpf telerik中的book控件
下载 telerik中的书本控件,仅供学习使用.
- [原创]在Framelayout中放置button控件出现的覆盖问题
android Framelayout(帧布局)是很常用的布局,主要用来处理需要多个view叠加显示的情况. 然而在使用中,我发现Framelayout中的Button控件,会挡住所有其他控件,而不论 ...
- (转)客户端触发Asp.net中服务端控件事件
第一章. Asp.net中服务端控件事件是如何触发的 Asp.net 中在客户端触发服务端事件分为两种情况: 一. WebControls中的Button 和HtmlControls中的Type为su ...
随机推荐
- openGL-计算机图形大作业中出现的几个错误及解决
错误一 错误现象:按动相应按键i和o无法在x轴和y轴移动camera,但按相应按键p可以在z轴移动camera. 错误原因:为了移动camera,设置了三个全局变量x.y.z,用于gluLookAt( ...
- La nuova tecnologia del puntatore laser
Il potente puntatore laser 20000 mW viene fornito di serie con gestione termica e driver laser di qu ...
- delphi压缩与解压_不需要特别的控件
unit unzip; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- 深入理解JVM(二)——内存模型、可见性、指令重排序
上一篇我们介绍了JVM的基本运行流程以及内存结构,对JVM有了初步的认识,这篇文章我们将根据JVM的内存模型探索java当中变量的可见性以及不同的java指令在并发时可能发生的指令重排序的情况. 内存 ...
- Akka-CQRS(3)- 再想多点,全面点
上篇我介绍了CQRS模式存写部分的具体实现和akka-persistence一些函数和消息的用法.在这篇本来是准备直接用一个具体的例子来示范CQRS模式编程,主要是写端,或者是数据采集端.想着模拟收银 ...
- Android Studio升级3.2.1后的合并XML出错的解决方案
升级到3.2.1版本之后,遇到了合并XML出错的问题.错误内容大概如下: 当大家看到这个问题的时候,可能会有一行是可以点击的文件目录,点击到报错的地方. 如果没有可以点击的地方,也可以根据目录和 “行 ...
- webpack严格模式!!!忽略
1. babel5 babel: { options: { blacklist: ["useStrict"], // ... }, // ... } 2. babel6 修改.ba ...
- apollo入门(一)
1. apollo入门(一) 1.1. 核心概念 1.1.1. 应用 注意:每个应用需要配置一个appid 1.1.2. 环境 dev 开发环境 fat 功能测试环境 uat 用户接受测试环境 pro ...
- MD5( 信息摘要算法)的概念原理及python代码的实现
简述: message-digest algorithm 5(信息-摘要算法).经常说的“MD5加密”,就是它→信息-摘要算法. md5,其实就是一种算法.可以将一个字符串,或文件,或压缩包,执行md ...
- 一文了解Python中的判断语句
判断(if)语句 目标 开发中的应用场景 if 语句体验 if 语句进阶 综合应用 01. 开发中的应用场景 生活中的判断几乎是无所不在的,我们每天都在做各种各样的选择,如果这样?如果那样?…… 程序 ...