经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
Simple WPF: WPF 自定义按钮外形
来源:cnblogs  作者:mrchip  时间:2024/7/8 9:55:57  对本文有异议

最新内容优先发布于个人博客:小虎技术分享站,随后逐步搬运到博客园。

WPF的按钮提供了Template模板,可以通过修改Template模板中的内容对按钮的样式进行自定义,完整代码Github自取。

使用Style定义扁平化的按钮样式

定义一个ButtonStyleDictonary.xaml资源字典文件,在Control Template中定义一个带Border的按钮,然后定义Trigger作为改变样式的触发器

  1. <Style x:Key="FlatButtonStyle" TargetType="Button">
  2. <Setter Property="Template">
  3. <Setter.Value>
  4. <ControlTemplate TargetType="Button">
  5. <Border x:Name="border" Background="{TemplateBinding Background}"
  6. BorderBrush="{TemplateBinding BorderBrush}"
  7. BorderThickness="{TemplateBinding BorderThickness}"
  8. SnapsToDevicePixels="True">
  9. <TextBlock Text="{TemplateBinding Content}"
  10. Foreground="{TemplateBinding Foreground}"
  11. VerticalAlignment="Center"
  12. HorizontalAlignment="Center"/>
  13. </Border>
  14. <ControlTemplate.Triggers>
  15. <Trigger Property="IsMouseOver" Value="True">
  16. <Setter TargetName="border" Property="Background" Value="#2f96b4"/>
  17. </Trigger>
  18. <Trigger Property="IsPressed" Value="True">
  19. <Setter TargetName="border" Property="Background" Value="red"/>
  20. </Trigger>
  21. </ControlTemplate.Triggers>
  22. </ControlTemplate>
  23. </Setter.Value>
  24. </Setter>
  25. </Style>

WPF中使用ResourceDictonary 资源字典

引入在资源字典文件中定义公共的Template,然后在xaml窗口、自定义控件或者整个App当中调用

  1. <Window.Resources>
  2. <ResourceDictionary>
  3. <ResourceDictionary.MergedDictionaries>
  4. <ResourceDictionary Source="ButtonStyleDictonary.xaml"></ResourceDictionary>
  5. </ResourceDictionary.MergedDictionaries>
  6. </ResourceDictionary>
  7. </Window.Resources>

然后就可以在窗体的xaml中应用刚才定义的属性了

  1. <Button Style="{StaticResource FlatButtonStyle}" Width="64" Height="28">
  2. Hello
  3. </Button>

使用Style和Polygon自定义Button的外形

  1. <Style x:Key="ArrowButtonStyle" TargetType="Button">
  2. <Setter Property="Template">
  3. <Setter.Value>
  4. <ControlTemplate TargetType="Button">
  5. <Polygon x:Name="border" Fill="{TemplateBinding Background}"
  6. Points="0,0 2,0 1,1" Stroke="Black" StrokeThickness="2"
  7. SnapsToDevicePixels="True"
  8. Stretch="Uniform"/>
  9. <ControlTemplate.Triggers>
  10. <Trigger Property="IsMouseOver" Value="True">
  11. <Setter TargetName="border" Property="Fill" Value="gray"/>
  12. </Trigger>
  13. <Trigger Property="IsPressed" Value="True">
  14. <Setter TargetName="border" Property="Fill" Value="red"/>
  15. </Trigger>
  16. </ControlTemplate.Triggers>
  17. </ControlTemplate>
  18. </Setter.Value>
  19. </Setter>
  20. </Style>

效果如下

image

参考资料

WPF自定义控件与样式-自定义按钮(Button)
如何:使用应用程序范围的资源字典

原文链接:https://www.cnblogs.com/mrchip/p/18288981

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号