本演练的目的是了解如何创建在 Windows Presentation Foundation (WPF) 应用程序中使用的动画按钮。 本演练使用样式和模板来创建自定义的按钮资源, 以允许在按钮声明中重复使用代码和按钮逻辑分离。 本演练完全是在中可扩展应用程序标记语言 (XAML)编写的。
Important: 本演练将指导你完成通过键入或复制并粘贴可扩展应用程序标记语言 (XAML)到 Microsoft Visual Studio 创建应用程序的步骤。 如果希望了解如何使用设计工具 (Microsoft Expression Blend) 创建相同的应用程序, 请参阅使用 Microsoft Expression Blend 创建按钮。
下图显示了 "已完成" 按钮。
使用 XAML 创建的自定义按钮
创建基本按钮
首先创建一个新项目, 并向该窗口添加几个按钮。
创建新的 WPF 项目并向窗口中添加按钮
-
启动 Visual Studio。
-
创建新的 WPF 项目: 在 “文件” 菜单上,指向 “新建” ,然后单击 “项目” 。 找到Windows 应用程序 (WPF) 模板, 并将项目命名为 "AnimatedButton"。 这将创建应用程序的主干。
-
添加基本的默认按钮: 此演练所需的所有文件都由模板提供。 在解决方案资源管理器中双击 Window1.xaml 文件, 将其打开。 默认情况下, window1.xaml 中xref:System.Windows.Controls.Grid有一个元素。 通过键入或复制以下突出显示的代码并可扩展应用程序标记语言 (XAML)将其粘贴到 window1.xaml 中, 删除元素并向页面添加几个按钮:xref:System.Windows.Controls.Grid
<Window x:Class="AnimatedButton.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="AnimatedButton" Height="300" Width="300" Background="Black"> <!-- Buttons arranged vertically inside a StackPanel. --> <StackPanel HorizontalAlignment="Left"> <Button>Button 1</Button> <Button>Button 2</Button> <Button>Button 3</Button> </StackPanel> </Window>按 F5 运行该应用程序;应会看到如下图所示的一组按钮。
三个基本按钮
现在, 您已创建了基本按钮, 接下来在 Window1.xaml 文件中完成工作。 本演练的其余部分侧重于 app.xaml 文件, 为按钮定义样式和模板。
设置基本属性
接下来, 让我们在这些按钮上设置一些属性, 以控制按钮的外观和布局。 您将使用资源来定义整个应用程序的按钮属性, 而不是单独设置按钮的属性。 应用程序资源在概念上类似于网页的外部级联样式表 (CSS);但是, 资源比级联样式表 (CSS) 要强大得多, 如本演练结束时所见。 若要了解有关资源的详细信息, 请参阅XAML 资源。
使用样式设置按钮的基本属性
-
定义应用程序 .Resources 块: 打开 app.xaml 并添加以下突出显示的标记 (如果尚未存在):
<Application x:Class="AnimatedButton.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="Window1.xaml" > <Application.Resources> <!-- Resources for the entire application can be defined here. --> </Application.Resources> </Application>资源范围由定义资源的位置确定。 在 app.xaml 文件
Application.Resources中定义中的资源, 可以从应用程序中的任何位置使用资源。 若要了解有关定义资源范围的详细信息, 请参阅XAML 资源。 -
创建样式, 并使用它定义基本属性值: 将以下标记添加到
Application.Resources块。 此标记创建一个xref:System.Windows.Style , 它应用于应用程序中的所有按钮, xref:System.Windows.FrameworkElement.Width*将按钮的设置为 90, xref:System.Windows.FrameworkElement.Margin*将设置为 10:<Application.Resources> <Style TargetType="Button"> <Setter Property="Width" Value="90" /> <Setter Property="Margin" Value="10" /> </Style> </Application.Resources>属性指定样式应用于所有类型xref:System.Windows.Controls.Button的对象。 xref:System.Windows.Style.TargetType* 每xref:System.Windows.Setter 个xref:System.Windows.Style为设置一个不同的属性值。 因此, 应用程序中的每个按钮的宽度均为 90, 边距为10。 如果按 F5 运行该应用程序, 将看到以下窗口。
宽度为 90, 边距为10的按钮
您还可以对样式进行更多的操作, 其中包括多种方法来微调目标对象、指定复杂的属性值, 甚至使用样式作为其他样式的输入。 有关详细信息,请参阅样式设置和模板化。
-
将样式属性值设置为资源: 通过资源, 可以使用一种简单的方法来重复使用通常定义的对象和值。 使用资源来定义复杂值非常有用, 使代码更具模块化。 将以下突出显示的标记添加到 app.config。
<Application.Resources> <LinearGradientBrush x:Key="GrayBlueGradientBrush" StartPoint="0,0" EndPoint="1,1"> <GradientStop Color="DarkGray" Offset="0" /> <GradientStop Color="#CCCCFF" Offset="0.5" /> <GradientStop Color="DarkGray" Offset="1" /> </LinearGradientBrush> <Style TargetType="{x:Type Button}"> <Setter Property="Background" Value="{StaticResource GrayBlueGradientBrush}" /> <Setter Property="Width" Value="80" /> <Setter Property="Margin" Value="10" /> </Style> </Application.Resources>直接在
Application.Resources块下面创建一个名为 "GrayBlueGradientBrush" 的资源。 此资源定义水平渐变。 此资源可用作应用程序中任意位置的属性值, 包括xref:System.Windows.Controls.Control.Background*属性在按钮样式 setter 中的值。 现在, 所有按钮的属性值xref:System.Windows.Controls.Control.Background*都是此渐变。按 F5 运行该应用程序。 其外观应如下所示。
带有渐变背景的按钮
创建定义按钮外观的模板
在本部分中, 将创建一个自定义按钮外观 (表示) 的模板。 按钮表示由多个对象组成, 其中包括矩形和其他组件, 用于为按钮指定独特的外观。
到目前为止, 控件在应用程序中的外观控件限制为更改按钮的属性。 如果要对按钮的外观进行更多的根式更改, 会怎么样? 模板可以强大地控制对象的表示形式。 由于可以在样式内使用模板, 因此可以将模板应用于样式应用到的所有对象 (在本演练中为按钮)。
使用模板定义按钮的外观
-
设置模板: 由于控件 ( xref:System.Windows.Controls.Button如xref:System.Windows.Controls.Control.Template*具有属性), 因此可以定义模板属性值, 就像xref:System.Windows.Style我们使用在中xref:System.Windows.Setter设置的其他属性值一样。 将以下突出显示的标记添加到按钮样式。
<Application.Resources> <LinearGradientBrush x:Key="GrayBlueGradientBrush" StartPoint="0,0" EndPoint="1,1"> <GradientStop Color="DarkGray" Offset="0" /> <GradientStop Color="#CCCCFF" Offset="0.5" /> <GradientStop Color="DarkGray" Offset="1" /> </LinearGradientBrush> <Style TargetType="{x:Type Button}"> <Setter Property="Background" Value="{StaticResource GrayBlueGradientBrush}" /> <Setter Property="Width" Value="80" /> <Setter Property="Margin" Value="10" /> <Setter Property="Template"> <Setter.Value> <!-- The button template is defined here. --> </Setter.Value> </Setter> </Style> </Application.Resources> -
更改按钮演示: 此时, 需要定义模板。 添加以下突出显示的标记。 此标记指定两xref:System.Windows.Shapes.Rectangle个具有舍入边缘的元素, xref:System.Windows.Controls.DockPanel后跟一个。 xref:System.Windows.Controls.DockPanel用于承载按钮的。 xref:System.Windows.Controls.ContentPresenter xref:System.Windows.Controls.ContentPresenter显示按钮的内容。 在本演练中, 内容是文本 ("Button 1"、"Button 2"、"Button 3")。 所有模板组件 (矩形和xref:System.Windows.Controls.DockPanel) 都在中xref:System.Windows.Controls.Grid进行布局。
<Setter.Value> <ControlTemplate TargetType="Button"> <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" ClipToBounds="True"> <!-- Outer Rectangle with rounded corners. --> <Rectangle x:Name="outerRectangle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stroke="{TemplateBinding Background}" RadiusX="20" RadiusY="20" StrokeThickness="5" Fill="Transparent" /> <!-- Inner Rectangle with rounded corners. --> <Rectangle x:Name="innerRectangle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stroke="Transparent" StrokeThickness="20" Fill="{TemplateBinding Background}" RadiusX="20" RadiusY="20" /> <!-- Present Content (text) of the button. --> <DockPanel Name="myContentPresenterDockPanel"> <ContentPresenter x:Name="myContentPresenter" Margin="20" Content="{TemplateBinding Content}" TextBlock.Foreground="Black" /> </DockPanel> </Grid> </ControlTemplate> </Setter.Value>按 F5 运行该应用程序。 其外观应如下所示。
-
将 glasseffect 添加到模板: 接下来, 你将添加玻璃。 首先, 创建一些创建玻璃渐变效果的资源。 在块中的
Application.Resources任何位置添加这些渐变资源:<Application.Resources> <GradientStopCollection x:Key="MyGlassGradientStopsResource"> <GradientStop Color="WhiteSmoke" Offset="0.2" /> <GradientStop Color="Transparent" Offset="0.4" /> <GradientStop Color="WhiteSmoke" Offset="0.5" /> <GradientStop Color="Transparent" Offset="0.75" /> <GradientStop Color="WhiteSmoke" Offset="0.9" /> <GradientStop Color="Transparent" Offset="1" /> </GradientStopCollection> <LinearGradientBrush x:Key="MyGlassBrushResource" StartPoint="0,0" EndPoint="1,1" Opacity="0.75" GradientStops="{StaticResource MyGlassGradientStopsResource}" /> <!-- Styles and other resources below here. -->这些资源xref:System.Windows.Shapes.Shape.Fill*用作在按钮模板的中插入xref:System.Windows.Controls.Grid的矩形的。 将以下突出显示的标记添加到模板。
<Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" ClipToBounds="True"> <!-- Outer Rectangle with rounded corners. --> <Rectangle x:Name="outerRectangle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stroke="{TemplateBinding Background}" RadiusX="20" RadiusY="20" StrokeThickness="5" Fill="Transparent" /> <!-- Inner Rectangle with rounded corners. --> <Rectangle x:Name="innerRectangle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stroke="Transparent" StrokeThickness="20" Fill="{TemplateBinding Background}" RadiusX="20" RadiusY="20" /> <!-- Glass Rectangle --> <Rectangle x:Name="glassCube" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" StrokeThickness="2" RadiusX="10" RadiusY="10" Opacity="0" Fill="{StaticResource MyGlassBrushResource}" RenderTransformOrigin="0.5,0.5"> <Rectangle.Stroke> <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> <LinearGradientBrush.GradientStops> <GradientStop Offset="0.0" Color="LightBlue" /> <GradientStop Offset="1.0" Color="Gray" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </Rectangle.Stroke> <!-- These transforms have no effect as they are declared here. The reason the transforms are included is to be targets for animation (see later). --> <Rectangle.RenderTransform> <TransformGroup> <ScaleTransform /> <RotateTransform /> </TransformGroup> </Rectangle.RenderTransform> <!-- A BevelBitmapEffect is applied to give the button a "Beveled" look. --> <Rectangle.BitmapEffect> <BevelBitmapEffect /> </Rectangle.BitmapEffect> </Rectangle> <!-- Present Text of the button. --> <DockPanel Name="myContentPresenterDockPanel"> <ContentPresenter x:Name="myContentPresenter" Margin="20" Content="{TemplateBinding Content}" TextBlock.Foreground="Black" /> </DockPanel> </Grid> </ControlTemplate> </Setter.Value>请注意,
x:Name属性为 "glassCube" 的矩形的为 0, 因此, 在运行该示例时, 看不到顶部显示的玻璃矩形。 xref:System.Windows.UIElement.Opacity* 这是因为, 稍后我们会将触发器添加到模板中, 以便用户与按钮交互时。 不过, 现在可以通过将xref:System.Windows.UIElement.Opacity*值更改为1并运行应用程序来查看按钮的外观。 请参见下图。 继续下一步之前, 请将xref:System.Windows.UIElement.Opacity*返回回0。使用 XAML 创建的自定义按钮
创建按钮交互性
在本部分中, 你将创建属性触发器和事件触发器, 以更改属性值, 并运行动画来响应用户操作, 例如, 将鼠标指针移到按钮上并单击。
添加交互 (鼠标悬停、鼠标离开、单击等) 的一种简单方法是在模板或样式中定义触发器。 若要创建xref:System.Windows.Trigger, 请定义属性 "条件", 如:按钮xref:System.Windows.UIElement.IsMouseOver*属性值true等于。 然后, 定义在触发器条件为 true 时执行的 setter (操作)。
若要创建按钮交互性
-
添加模板触发器: 将突出显示的标记添加到模板。
<Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" ClipToBounds="True"> <!-- Outer Rectangle with rounded corners. --> <Rectangle x:Name="outerRectangle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stroke="{TemplateBinding Background}" RadiusX="20" RadiusY="20" StrokeThickness="5" Fill="Transparent" /> <!-- Inner Rectangle with rounded corners. --> <Rectangle x:Name="innerRectangle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stroke="Transparent" StrokeThickness="20" Fill="{TemplateBinding Background}" RadiusX="20" RadiusY="20" /> <!-- Glass Rectangle --> <Rectangle x:Name="glassCube" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" StrokeThickness="2" RadiusX="10" RadiusY="10" Opacity="0" Fill="{StaticResource MyGlassBrushResource}" RenderTransformOrigin="0.5,0.5"> <Rectangle.Stroke> <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> <LinearGradientBrush.GradientStops> <GradientStop Offset="0.0" Color="LightBlue" /> <GradientStop Offset="1.0" Color="Gray" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </Rectangle.Stroke> <!-- These transforms have no effect as they are declared here. The reason the transforms are included is to be targets for animation (see later). --> <Rectangle.RenderTransform> <TransformGroup> <ScaleTransform /> <RotateTransform /> </TransformGroup> </Rectangle.RenderTransform> <!-- A BevelBitmapEffect is applied to give the button a "Beveled" look. --> <Rectangle.BitmapEffect> <BevelBitmapEffect /> </Rectangle.BitmapEffect> </Rectangle> <!-- Present Text of the button. --> <DockPanel Name="myContentPresenterDockPanel"> <ContentPresenter x:Name="myContentPresenter" Margin="20" Content="{TemplateBinding Content}" TextBlock.Foreground="Black" /> </DockPanel> </Grid> <ControlTemplate.Triggers> <!-- Set action triggers for the buttons and define what the button does in response to those triggers. --> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> -
添加属性触发器: 将突出显示的标记添加
ControlTemplate.Triggers到块:<ControlTemplate.Triggers> <!-- Set properties when mouse pointer is over the button. --> <Trigger Property="IsMouseOver" Value="True"> <!-- Below are three property settings that occur when the condition is met (user mouses over button). --> <!-- Change the color of the outer rectangle when user mouses over it. --> <Setter Property ="Rectangle.Stroke" TargetName="outerRectangle" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" /> <!-- Sets the glass opacity to 1, therefore, the glass "appears" when user mouses over it. --> <Setter Property="Rectangle.Opacity" Value="1" TargetName="glassCube" /> <!-- Makes the text slightly blurry as though you were looking at it through blurry glass. --> <Setter Property="ContentPresenter.BitmapEffect" TargetName="myContentPresenter"> <Setter.Value> <BlurBitmapEffect Radius="1" /> </Setter.Value> </Setter> </Trigger> <ControlTemplate.Triggers/>按 F5 运行应用程序, 并在按钮上运行鼠标指针时查看效果。
-
添加焦点触发器: 接下来, 我们将添加一些类似的 setter 来处理按钮有焦点的情况 (例如, 用户单击后)。
<ControlTemplate.Triggers> <!-- Set properties when mouse pointer is over the button. --> <Trigger Property="IsMouseOver" Value="True"> <!-- Below are three property settings that occur when the condition is met (user mouses over button). --> <!-- Change the color of the outer rectangle when user mouses over it. --> <Setter Property ="Rectangle.Stroke" TargetName="outerRectangle" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" /> <!-- Sets the glass opacity to 1, therefore, the glass "appears" when user mouses over it. --> <Setter Property="Rectangle.Opacity" Value="1" TargetName="glassCube" /> <!-- Makes the text slightly blurry as though you were looking at it through blurry glass. --> <Setter Property="ContentPresenter.BitmapEffect" TargetName="myContentPresenter"> <Setter.Value> <BlurBitmapEffect Radius="1" /> </Setter.Value> </Setter> </Trigger> <!-- Set properties when button has focus. --> <Trigger Property="IsFocused" Value="true"> <Setter Property="Rectangle.Opacity" Value="1" TargetName="glassCube" /> <Setter Property="Rectangle.Stroke" TargetName="outerRectangle" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" /> <Setter Property="Rectangle.Opacity" Value="1" TargetName="glassCube" /> </Trigger> </ControlTemplate.Triggers>按 F5 运行应用程序, 并单击其中一个按钮。 请注意, 单击按钮后, 按钮会保持突出显示, 因为它仍有焦点。 如果单击另一个按钮, 新按钮会获得焦点, 而最后一个按钮丢失。
-
为xref:System.Windows.UIElement.MouseEnter和添加xref:System.Windows.UIElement.MouseLeave动画 : 接下来, 将一些动画添加到触发器。 将以下标记添加到
ControlTemplate.Triggers块内的任意位置。<!-- Animations that start when mouse enters and leaves button. --> <EventTrigger RoutedEvent="Mouse.MouseEnter"> <EventTrigger.Actions> <BeginStoryboard Name="mouseEnterBeginStoryboard"> <Storyboard> <!-- This animation makes the glass rectangle shrink in the X direction. --> <DoubleAnimation Storyboard.TargetName="glassCube" Storyboard.TargetProperty= "(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" By="-0.1" Duration="0:0:0.5" /> <!-- This animation makes the glass rectangle shrink in the Y direction. --> <DoubleAnimation Storyboard.TargetName="glassCube" Storyboard.TargetProperty= "(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" By="-0.1" Duration="0:0:0.5" /> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> <EventTrigger RoutedEvent="Mouse.MouseLeave"> <EventTrigger.Actions> <!-- Stopping the storyboard sets all animated properties back to default. --> <StopStoryboard BeginStoryboardName="mouseEnterBeginStoryboard" /> </EventTrigger.Actions> </EventTrigger>当鼠标指针移到按钮上并在指针离开时返回到正常大小时, 玻璃矩形会收缩。
当指针进入按钮 (xref:System.Windows.UIElement.MouseEnter引发事件) 时, 会触发两个动画。 这些动画沿 X 和 Y 轴收缩玻璃矩形。 请注意xref:System.Windows.Media.Animation.DoubleAnimation元素上的属性, xref:System.Windows.Media.Animation.Timeline.Duration*即xref:System.Windows.Media.Animation.DoubleAnimation.By*和。 指定动画每半秒出现一次, 并xref:System.Windows.Media.Animation.DoubleAnimation.By*指定玻璃缩小 10%。 xref:System.Windows.Media.Animation.Timeline.Duration*
第二个事件触发器xref:System.Windows.UIElement.MouseLeave() 只是停止第一个触发器。 停止xref:System.Windows.Media.Animation.Storyboard时, 所有动画属性都将恢复为其默认值。 因此, 当用户将指针移到按钮上时, 按钮返回到鼠标指针移到按钮上之前的状态。 有关动画的详细信息, 请参阅动画概述。
-
在单击按钮时添加动画: 最后一步是在用户单击按钮时添加触发器。 在
ControlTemplate.Triggers块中的任意位置添加以下标记:<!-- Animation fires when button is clicked, causing glass to spin. --> <EventTrigger RoutedEvent="Button.Click"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="glassCube" Storyboard.TargetProperty= "(Rectangle.RenderTransform).(TransformGroup.Children)[1].(RotateTransform.Angle)" By="360" Duration="0:0:0.5" /> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger>按 F5 运行应用程序, 然后单击其中一个按钮。 单击按钮时, 玻璃矩形会旋转。
总结
在本演练中, 您执行了以下练习:
-
目标为对象类型 (xref:System.Windows.Controls.Button)。 xref:System.Windows.Style
-
使用的xref:System.Windows.Style整个应用程序中的按钮的受控基本属性。
-
创建了一些资源, 如用于资源xref:System.Windows.Style库属性值的渐变。
-
通过将模板应用于按钮, 自定义了整个应用程序中的按钮的外观。
-
用于响应用户操作 (如xref:System.Windows.UIElement.MouseEnter、 xref:System.Windows.UIElement.MouseLeave和xref:System.Windows.Controls.Primitives.ButtonBase.Click) 的按钮的自定义行为, 其中包含动画效果。
请参阅
历史资料。此文对应 2019-08-18 的中文文档快照,适用产品版本和命令行为以原文为准。
来源与署名:Microsoft 和文档贡献者。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站展开固定版本仓库内的引用内容,将 Markdown 转为 HTML,规范化链接,移除外部媒体和导航标记。
