前面几篇文章,实现了窗口样式的修改,下面开始往这个窗口里面添加各种控件,当然,所有控件都会使用自定义样式,代码也会在下方详细地列出。

窗口模板的git地址:https://github.com/gxygit/WpfTemplate

有兴趣的小伙伴,下载下来接着做喽~

wpf提供了默认的button样式,先看一下:

WPF 自定义Button样式_自定义样式

按钮长的。。还算可以吧。。。

但是这怎么能满足呢,下面开始编写按钮样式咯~



<Style x:Key="TextRectButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Border Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
Background="{TemplateBinding Background}"
BorderBrush="Black"
BorderThickness="0.8"
CornerRadius="3"
Padding="2"
x:Name="border"
>
<TextBlock Text="{TemplateBinding Content}"
VerticalAlignment="Center" HorizontalAlignment="Center"
Foreground="{TemplateBinding Foreground}"
FontSize="{TemplateBinding FontSize}"
FontFamily="{TemplateBinding FontFamily}"
x:Name="text"
/>
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard >
<ColorAnimation To="#57A64A" Duration="0:0:0.3" Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color"/>
<ColorAnimation To="#FFFFFF" Duration="0:0:0.3" Storyboard.TargetName="text" Storyboard.TargetProperty="Foreground.Color"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard >
<ColorAnimation To="#0057A64A" Duration="0:0:0.3" Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color"/>
<ColorAnimation To="#000000" Duration="0:0:0.3" Storyboard.TargetName="text" Storyboard.TargetProperty="Foreground.Color"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

  样式写好以后,在窗口里面加上引用就行咯~

WPF 自定义Button样式_github_02


来看一下效果吧~

WPF 自定义Button样式_自定义样式_03

当然,如果你觉得颜色不合适,可以自行修改~


这次就先写到这里了,欢迎吐槽~