1.文件创建与代码实现
打开 Wpf_Examples 项目,在自定义按钮类库中创建 ImageTextButton.cs 文件 和 ImageTextButton.xaml 资源样式文件。完成后如下图所示:
ImageTextButton.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.Media;
namespace CustomControlLib.Buttons
{
public class ImageTextButton : Button
{
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ImageTextButton));
public ImageSource ImageSource
{
get => (ImageSource)GetValue(ImageSourceProperty);
set => SetValue(ImageSourceProperty, value);
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(ImageTextButton));
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public static readonly DependencyProperty IsDashedBorderProperty =
DependencyProperty.Register("IsDashedBorder", typeof(bool), typeof(ImageTextButton), new PropertyMetadata(false, OnIsDashedBorderChanged));
public bool IsDashedBorder
{
get => (bool)GetValue(IsDashedBorderProperty);
set => SetValue(IsDashedBorderProperty, value);
}
private static void OnIsDashedBorderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ImageTextButton)d).InvalidateVisual();
}
public ImageTextButton()
{
DefaultStyleKey = typeof(ImageTextButton);
}
}
}
ImageTextButton.xaml 样式代码如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomControlLib.Buttons">
<Style TargetType="{x:Type local:ImageTextButton}">
<Setter Property="Width" Value="60"/>
<Setter Property="Height" Value="60"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ImageTextButton}">
<Border x:Name="Border"
BorderBrush="Gray"
BorderThickness="1"
CornerRadius="8"
Background="Transparent"
SnapsToDevicePixels="true" Opacity="1" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="65*"/>
<RowDefinition Height="35*"/>
</Grid.RowDefinitions>
<Image x:Name="Image" Grid.Row="0"
Source="{TemplateBinding ImageSource}"
HorizontalAlignment="Center"
VerticalAlignment="Center" Margin="0 5 0 0" Opacity="1"/>
<TextBlock x:Name="TextBlock" Grid.Row="1"
Text="{TemplateBinding Text}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="14"
Foreground="Black" Opacity="1"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightGray" TargetName="Border"/>
<Setter Property="BorderThickness" Value="0" TargetName="Border"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="DarkGray" TargetName="Border"/>
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Background" Value="DarkGray" TargetName="Border"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Gray"/>
<Setter Property="Background" Value="LightGray" TargetName="Border"/>
</Trigger>
<Trigger Property="IsDashedBorder" Value="True">
<Setter Property="IsEnabled" Value="false"/>
<Setter Property="Opacity" Value="0.6" TargetName="Border"/>
<Setter Property="Opacity" Value="0.6" TargetName="Image"/>
<Setter Property="Opacity" Value="0.6" TargetName="TextBlock"/>
<Setter Property="Foreground" Value="Gray"/>
<Setter Property="BorderBrush" TargetName="Border">
<Setter.Value>
<VisualBrush Stretch="Fill" TileMode="Tile">
<VisualBrush.Visual>
<Rectangle StrokeDashArray="2 1" Stroke="Gray" StrokeThickness="1" Width="50" Height="50"/>
</VisualBrush.Visual>
</VisualBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
2、样式引用与封装
完成按钮样式后,只需要在 Generic.xaml 文件中引用 按钮样式即完成整个按钮的封装了,这样只要项目引用 自定义按钮库即可使用该自定义按钮了。
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/CustomControlLib;component/Themes/Buttons/IconFontButton.xaml" />
<ResourceDictionary Source="pack://application:,,,/CustomControlLib;component/Themes/Buttons/FontIconButton.xaml" />
<ResourceDictionary Source="pack://application:,,,/CustomControlLib;component/Themes/Buttons/ImageTextButton.xaml" />
</ResourceDictionary.MergedDictionaries>