当前位置:   article > 正文

WPF自定义控件(教程含源码)-结束iconfont实现带图标的按钮_wpf 图标按钮

wpf 图标按钮
  • 先来看看最终效果

  • 实现步骤

  1. 在阿里巴巴矢量图标库选取需要的图标

阿里巴巴矢量图标库

点击下载至本地后即可获取到 iconfont.ttf 字体文件

将字体文件拷贝到项目内, 如下我放在 Contents 文件夹下

  1. 显示这些矢量图标

在项目内新建资源字典“RText.xaml”,将fontfamily 设置为contents文件夹内的字体

  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns:local="clr-namespace:WPFCustomControl.Controls">
  4. <Style TargetType="TextBlock" x:Key="RText">
  5. <Setter Property="FontFamily" Value="/WPFCustomControl;component/Contents/#iconfont" />
  6. <Setter Property="TextAlignment" Value="Center"/>
  7. <Setter Property="HorizontalAlignment" Value="Center" />
  8. <Setter Property="VerticalAlignment" Value="Center" />
  9. <Setter Property="FontSize" Value="20" />
  10. <Setter Property="Text" Value="&#xe601;" />
  11. </Style>
  12. </ResourceDictionary>

新建一个用户控件, 在里面使用上面新加的样式

  1. <Grid Background="White">
  2. <StackPanel>
  3. <WrapPanel >
  4. <TextBlock Text="&#xe601;" Style="{StaticResource RText}" Margin="10" />
  5. <TextBlock Text="&#xe602;" Style="{StaticResource RText}" Margin="10" />
  6. <TextBlock Text="&#xe603;" Style="{StaticResource RText}" Margin="10" />
  7. <TextBlock Text="&#xe604;" Style="{StaticResource RText}" Margin="10" />
  8. <TextBlock Text="&#xe605;" Style="{StaticResource RText}" Margin="10" />
  9. </WrapPanel>
  10. </StackPanel>
  11. </Grid>

效果如下

  1. 在button 中使用矢量图标

新建一个RButton类,继承Button。添加依赖属性 Icon。

  1. public partial class RButton:Button {
  2. static RButton() {
  3. DefaultStyleKeyProperty.OverrideMetadata(typeof(RButton), new FrameworkPropertyMetadata(typeof(RButton)));
  4. }
  5. #region Icon
  6. public static readonly DependencyProperty IconProperty =
  7. DependencyProperty.Register("Icon", typeof(string), typeof(RButton), new PropertyMetadata(""));
  8. public string Icon {
  9. get => (string)GetValue(IconProperty);
  10. set => SetValue(IconProperty, value);
  11. }
  12. #endregion
  13. }

为RButton 添加新的样式文件, 添加两种样式,一种纯色背景,一种白色背景带边框

  1. <!--纯色背景-->
  2. <Style TargetType="local:RButton" x:Key="btn">
  3. <Setter Property="Foreground" Value="White" />
  4. <Setter Property="Template">
  5. <Setter.Value>
  6. <ControlTemplate TargetType="local:RButton">
  7. <Border SnapsToDevicePixels="True" Background="{TemplateBinding Background}">
  8. <WrapPanel VerticalAlignment="Center" HorizontalAlignment="Center">
  9. <TextBlock Style="{StaticResource RText}" Text="{TemplateBinding Icon}" Margin="0 0 10 0" FontSize="{TemplateBinding FontSize}"/>
  10. <ContentPresenter SnapsToDevicePixels="True" />
  11. </WrapPanel>
  12. </Border>
  13. </ControlTemplate>
  14. </Setter.Value>
  15. </Setter>
  16. </Style>
  17. <!--白色背景,带边框-->
  18. <Style TargetType="local:RButton" x:Key="btnBorder">
  19. <Setter Property="Template">
  20. <Setter.Value>
  21. <ControlTemplate TargetType="local:RButton">
  22. <Border SnapsToDevicePixels="True" Background="White" BorderThickness="1" BorderBrush="{TemplateBinding Foreground}">
  23. <WrapPanel VerticalAlignment="Center" HorizontalAlignment="Center">
  24. <TextBlock Style="{StaticResource RText}" Text="{TemplateBinding Icon}" Margin="0 0 10 0" FontSize="{TemplateBinding FontSize}"
  25. Foreground="{TemplateBinding Foreground}"/>
  26. <ContentPresenter SnapsToDevicePixels="True" />
  27. </WrapPanel>
  28. </Border>
  29. </ControlTemplate>
  30. </Setter.Value>
  31. </Setter>
  32. </Style>

添加用户控件,在界面内使用新增的按钮样式

  1. <Grid Background="White">
  2. <StackPanel>
  3. <WrapPanel >
  4. <TextBlock Text="&#xe601;" Style="{StaticResource RText}" Margin="10" />
  5. <TextBlock Text="&#xe602;" Style="{StaticResource RText}" Margin="10" />
  6. <TextBlock Text="&#xe603;" Style="{StaticResource RText}" Margin="10" />
  7. <TextBlock Text="&#xe604;" Style="{StaticResource RText}" Margin="10" />
  8. <TextBlock Text="&#xe605;" Style="{StaticResource RText}" Margin="10" />
  9. </WrapPanel>
  10. <WrapPanel >
  11. <lc:RButton Icon="&#xe601;" Content="更多" Style="{StaticResource btn}" Background="#009688" Height="40" Width="100" Margin="10" FontSize="18"/>
  12. <lc:RButton Icon="&#xe602;" Content="更多" Style="{StaticResource btn}" Background="#1E9FFF" Height="40" Width="100" Margin="10" FontSize="18"/>
  13. <lc:RButton Icon="&#xe603;" Content="编辑" Style="{StaticResource btn}" Background="#FF5722" Height="40" Width="100" Margin="10" FontSize="18"/>
  14. <lc:RButton Icon="&#xe604;" Content="时间" Style="{StaticResource btn}" Background="#FFB800" Height="40" Width="100" Margin="10" FontSize="18"/>
  15. </WrapPanel>
  16. <WrapPanel >
  17. <lc:RButton Icon="&#xe601;" Content="更多" Style="{StaticResource btnBorder}" Foreground="#009688" Height="40" Width="100" Margin="10" FontSize="18"/>
  18. <lc:RButton Icon="&#xe602;" Content="更多" Style="{StaticResource btnBorder}" Foreground="#1E9FFF" Height="40" Width="100" Margin="10" FontSize="18"/>
  19. <lc:RButton Icon="&#xe603;" Content="编辑" Style="{StaticResource btnBorder}" Foreground="#FF5722" Height="40" Width="100" Margin="10" FontSize="18"/>
  20. <lc:RButton Icon="&#xe604;" Content="时间" Style="{StaticResource btnBorder}" Foreground="#FFB800" Height="40" Width="100" Margin="10" FontSize="18"/>
  21. </WrapPanel>
  22. </StackPanel>
  23. </Grid>

最终效果如下

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/825856
推荐阅读
相关标签
  

闽ICP备14008679号