背景:当前WPF的版本当中,对界面的要求越来越高,在此举例制作WPF的自定义按钮。
效果如下,分别是正常状态,悬浮状态和按下状态
1.Blend进行绘制
1.1 打开Blend For Visual Studio软件,创建WPF应用程序
1.2 首先是往Window中拖出1个按钮
1.3 右键按钮,选择【编辑副本】,命名样式为【ButtonTestStyle】
1.4 剪切保留,contentpresenter
1.5 删除border
1.6 在对应的template处拖出Grid控件容器
1.7 再把contentpresenter粘贴到Grid
1.8 添加一个矩形,拖出到Window
1.9 接着是对矩形的一个样式绘图,这里跟PS有点相似,具体要看个人发挥,以下是个人发挥的效果:
2.0 绘制完第一版的样式后,则需要对其他按钮的状态样式进行绘制,建议是根据状态不同,绘制不同的颜色,分别是IsDefault、IsMourseOver、IsPressed这三个就够了。
2.1 点击左上角的【启动】按钮,即可实现效果。
Blend绘制后,对应的XAML代码如下:
<Window x:Class="UserButton.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:UserButton"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="FocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
<SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>
<SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/>
<SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/>
<SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
<SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
<SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>
<Style x:Key="ButtonTestStyle" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
<Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
<Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Margin="2.461,3.939,26.583,11.814">
<Rectangle x:Name="rectangle" Margin="0,0,5.415,0" Stroke="#FFFBF9F9" StrokeThickness="2">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0.965"/>
<GradientStop Color="#FF1941F7" Offset="0.524"/>
<GradientStop Color="#FF1230BA" Offset="0.361"/>
<GradientStop Color="#FF1334C8" Offset="0.321"/>
<GradientStop Color="#FF020618" Offset="0.024"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter x:Name="contentPresenter" Focusable="False" Margin="0,0,0,0" TextElement.Foreground="White" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsDefaulted" Value="true"/>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Fill" TargetName="rectangle">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0.965"/>
<GradientStop Color="#FF1941F7" Offset="0.524"/>
<GradientStop Color="#FF1230BA" Offset="0.361"/>
<GradientStop Color="#FF1334C8" Offset="0.321"/>
<GradientStop Color="#FF5D74DC" Offset="0.024"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Fill" TargetName="rectangle">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="1"/>
<GradientStop Color="#FF1941F7" Offset="0.947"/>
<GradientStop Color="#FF1230BA" Offset="0.361"/>
<GradientStop Color="#FF1334C8" Offset="0.321"/>
<GradientStop Color="#FFE4E5EE" Offset="0.024"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="false"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button x:Name="button" Content="Button" Foreground="White" Margin="136.296,97.543,173.953,132.357" Style="{DynamicResource ButtonTestStyle}"/>
</Grid>
</Window>
2.在Visual Studio中,将Blend的样式,形成自定义控件。
2.1 新建项目和对应的类库
2.2 创建Controls 和Themes文件夹
2.3 在Controls文件加内添加WPF自定义控件,CommControlButtonTest
2.4 将以上Blend软件绘制的样式,复制到CommControlButtonTest的xmal文件内,注意需要修改文件的大标签格式为ResourceDictionary,总体采用这个标签来识别自定义的控件,接着将Blend软件绘制的样式内的Style复制到文件内。
xmal如下
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LibraryControls.Controls">
<Style TargetType="{x:Type local:CommControlButtonTest}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Margin="2.461,3.939,26.583,11.814">
<Rectangle x:Name="rectangle" Margin="0,0,5.415,0" Stroke="#FFFBF9F9" StrokeThickness="2">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0.965"/>
<GradientStop Color="#FF1941F7" Offset="0.524"/>
<GradientStop Color="#FF1230BA" Offset="0.361"/>
<GradientStop Color="#FF1334C8" Offset="0.321"/>
<GradientStop Color="#FF020618" Offset="0.024"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter x:Name="contentPresenter" Focusable="False" Margin="0,0,0,0" TextElement.Foreground="White" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsDefaulted" Value="true"/>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Fill" TargetName="rectangle">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0.965"/>
<GradientStop Color="#FF1941F7" Offset="0.524"/>
<GradientStop Color="#FF1230BA" Offset="0.361"/>
<GradientStop Color="#FF1334C8" Offset="0.321"/>
<GradientStop Color="#FF5D74DC" Offset="0.024"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Fill" TargetName="rectangle">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="1"/>
<GradientStop Color="#FF1941F7" Offset="0.947"/>
<GradientStop Color="#FF1230BA" Offset="0.361"/>
<GradientStop Color="#FF1334C8" Offset="0.321"/>
<GradientStop Color="#FFE4E5EE" Offset="0.024"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="false"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
cs代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace LibraryControls.Controls
{
/// <summary>
/// </summary>
public class CommControlButtonTest : Button
{
static CommControlButtonTest()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CommControlButtonTest), new FrameworkPropertyMetadata(typeof(CommControlButtonTest)));
}
}
}
2.5 在Themes文件夹内添加目录文件Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LibraryControls">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/LibraryControls;component/Controls/CommControlButtonTest.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
3.效果如下