WPF自定义控件(一)——Button
2020-12-13 15:19
标签:style blog http io color ar os for sp 接触WPF也有两个多月了,有了一定的理论基础和项目经验,现在打算写一个系列,做出来一个WPF的控件库。一方面可以加强自己的水平,另一方面可以给正在学习WPF的同行一个参考。本人水平有限,难免有一些错误,望各位指出! 先上图看看各种效果: 这个Button是我继承系统Button后扩展的,主要实现了:可设置悬浮和按下时的背景,可改变形状,并可设置按钮按下后保持锁定状态等功能。 这个Button我命名为XButton,扩展的所有属性我都会以X开头命名。好了,具体的东西看代码吧! 先来Xaml的: 其中的StyleFocusVisual是用来定义按Tab到这个控件上的样式的,代码如下: 接下来是CS的: 看了代码上的注释应该都能明白吧!要是有不明白的可以留言。 至于源代码,我之后会整理几个控件后一起发上来的! WPF自定义控件(一)——Button 标签:style blog http io color ar os for sp 原文地址:http://www.cnblogs.com/QinYK/p/4075020.html 1 ResourceDictionary
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:ctrl="clr-namespace:KAN.WPF.XCtrl.Controls">
5 Style x:Key="{x:Type ctrl:XButton}" TargetType="{x:Type ctrl:XButton}">
6 Style.Resources>
7 ResourceDictionary Source="/KAN.WPF.Xctrl;component/Themes/CommonStyle.xaml"/>
8 Style.Resources>
9 Setter Property="FocusVisualStyle" Value="{StaticResource StyleFocusVisual}"/>
10 Setter Property="Background" Value="White"/>
11 Setter Property="BorderBrush" Value="Silver"/>
12 Setter Property="BorderThickness" Value="1"/>
13 Setter Property="Control.Template">
14 Setter.Value>
15 ControlTemplate TargetType="{x:Type ctrl:XButton}">
16
17 Grid>
18 Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
19
20 Path x:Name="bdrButton"
21 Data="{Binding XShape, RelativeSource={RelativeSource TemplatedParent}}"
22 Stroke="{Binding XStrokeBrush, RelativeSource={RelativeSource TemplatedParent}}"
23 StrokeThickness="{Binding XStrokeThickness, RelativeSource={RelativeSource TemplatedParent}}"
24 Stretch="Fill" RenderTransformOrigin="0.5,0.5" Fill="{TemplateBinding Control.Background}">
25 Path.RenderTransform>
26 TransformGroup>
27 ScaleTransform/>
28 SkewTransform/>
29 RotateTransform/>
30 TranslateTransform/>
31 TransformGroup>
32 Path.RenderTransform>
33 Path>
34 Border>
35 ContentPresenter Name="contentPresenter" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
36 ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Focusable="False" RecognizesAccessKey="True"
37 SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" Content="{TemplateBinding ContentControl.Content}"
38 HorizontalAlignment="Center" VerticalAlignment="Center" />
39 Grid>
40
41 ControlTemplate.Triggers>
42
43 Trigger Property="UIElement.IsMouseOver" Value="True" >
44 Setter TargetName="bdrButton" Value="{Binding XMoverBrush, RelativeSource={RelativeSource TemplatedParent}}" Property="Path.Fill" />
45 Trigger>
46
47 Trigger Property="ButtonBase.IsPressed" Value="True">
48 Setter TargetName="bdrButton" Value="{Binding XEnterBrush, RelativeSource={RelativeSource TemplatedParent}}" Property="Path.Fill" />
49 Trigger>
50
51 Trigger Property="IsEnabled" Value="false">
52 Setter TargetName="bdrButton" Value="{Binding XUnEnabledBrush, RelativeSource={RelativeSource TemplatedParent}}" Property="Path.Fill" />
53 Trigger>
54
55 MultiTrigger>
56 MultiTrigger.Conditions>
57 Condition Property="IsFocused" Value="True"/>
58 Condition Property="XIsFoucedBrushLock" Value="True"/>
59 MultiTrigger.Conditions>
60 MultiTrigger.Setters>
61 Setter TargetName="bdrButton" Value="{Binding XEnterBrush, RelativeSource={RelativeSource TemplatedParent}}" Property="Path.Fill" />
62 MultiTrigger.Setters>
63 MultiTrigger>
64 ControlTemplate.Triggers>
65 ControlTemplate>
66 Setter.Value>
67 Setter>
68 Style>
69 ResourceDictionary>
1 ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
3 Style x:Key="StyleFocusVisual">
4 Setter Property="Control.Template">
5 Setter.Value>
6 ControlTemplate>
7 Border Margin="0" BorderBrush="#FF9FBDF4" BorderThickness="1"/>
8 ControlTemplate>
9 Setter.Value>
10 Setter>
11 Style>
12 ResourceDictionary>
1 using System;
2 using System.Windows;
3 using System.Windows.Controls;
4 using System.Windows.Media;
5 using System.Windows.Shapes;
6 using System.Windows.Media.Imaging;
7
8 namespace KAN.WPF.XCtrl.Controls
9 {
10 ///