wpf 自定義圓形按鈕


wpf 自定義圓形按鈕

效果圖

默認樣式

獲取焦點樣式

點擊樣式

 

下面是實現代碼:

一個是自定義控件類,一個是控件類皮膚

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Windows;
  7 using System.Windows.Controls;
  8 using System.Windows.Data;
  9 using System.Windows.Documents;
 10 using System.Windows.Input;
 11 using System.Windows.Media;
 12 using System.Windows.Media.Imaging;
 13 using System.Windows.Navigation;
 14 using System.Windows.Shapes;
 15 
 16 namespace MF.WPF.CustomControls.RoundButton
 17 {
 18     /// <summary>
 19     /// 按照步驟 1a 或 1b 操作,然后執行步驟 2 以在 XAML 文件中使用此自定義控件。
 20     ///
 21     /// 步驟 1a) 在當前項目中存在的 XAML 文件中使用該自定義控件。
 22     /// 將此 XmlNamespace 特性添加到要使用該特性的標記文件的根 
 23     /// 元素中: 
 24     ///
 25     ///     xmlns:MyNamespace="clr-namespace:MF.WPF.CustomControls.RoundButton"
 26     ///
 27     ///
 28     /// 步驟 1b) 在其他項目中存在的 XAML 文件中使用該自定義控件。
 29     /// 將此 XmlNamespace 特性添加到要使用該特性的標記文件的根 
 30     /// 元素中: 
 31     ///
 32     ///     xmlns:MyNamespace="clr-namespace:MF.WPF.CustomControls.RoundButton;assembly=MF.WPF.CustomControls.RoundButton"
 33     ///
 34     /// 您還需要添加一個從 XAML 文件所在的項目到此項目的項目引用,
 35     /// 並重新生成以避免編譯錯誤: 
 36     ///
 37     ///     在解決方案資源管理器中右擊目標項目,然后依次單擊
 38     ///     “添加引用”->“項目”->[瀏覽查找並選擇此項目]
 39     ///
 40     ///
 41     /// 步驟 2)
 42     /// 繼續操作並在 XAML 文件中使用控件。
 43     ///
 44     ///     <MyNamespace:RoundButton/>
 45     ///
 46     /// </summary>
 47     /// 
 48     public class RoundButton : Button
 49     {
 50        
 51         public static readonly DependencyProperty EllipseDiameterProperty = DependencyProperty.Register("EllipseDiameter", typeof(double), typeof(RoundButton), new PropertyMetadata(22D));
 52      
 53         public static readonly DependencyProperty EllipseStrokeThicknessProperty = DependencyProperty.Register("EllipseStrokeThickness", typeof(double), typeof(RoundButton), new PropertyMetadata(1D));
 54       
 55         public static readonly DependencyProperty IconDataProperty = DependencyProperty.Register("IconData", typeof(Geometry), typeof(RoundButton));
 56        
 57         public static readonly DependencyProperty IconSizeProperty = DependencyProperty.Register("IconSize", typeof(double), typeof(RoundButton), new PropertyMetadata(12D));
 58          
 59         static RoundButton()
 60         {
 61             DefaultStyleKeyProperty.OverrideMetadata(typeof(RoundButton), new FrameworkPropertyMetadata(typeof(RoundButton)));
 62         }
 63 
 64         /// <summary>
 65         /// 獲取或設置橢圓直徑。
 66         /// </summary>
 67         [Description("獲取或設置橢圓直徑")]
 68         [Category("Common Properties")]
 69         public double EllipseDiameter
 70         {
 71             get { return (double)GetValue(EllipseDiameterProperty); }
 72             set { SetValue(EllipseDiameterProperty, value); }
 73         }
 74 
 75         /// <summary>
 76         /// 獲取或設置橢圓筆觸粗細。
 77         /// </summary>
 78         [Description("獲取或設置橢圓筆觸粗細")]
 79         [Category("Common Properties")]
 80         public double EllipseStrokeThickness
 81         {
 82             get { return (double)GetValue(EllipseStrokeThicknessProperty); }
 83             set { SetValue(EllipseStrokeThicknessProperty, value); }
 84         }
 85 
 86         /// <summary>
 87         /// 獲取或設置圖標路徑數據。
 88         /// </summary>        
 89         [Description("獲取或設置圖標路徑數據")]
 90         [Category("Common Properties")]
 91         public Geometry IconData
 92         {
 93             get { return (Geometry)GetValue(IconDataProperty); }
 94             set { SetValue(IconDataProperty, value); }
 95         }
 96 
 97         /// <summary>
 98         ///獲取或設置icon圖標的高和寬。
 99         /// </summary>       
100         [Description("獲取或設置icon圖標的高和寬")]
101         [Category("Common Properties")]
102         public double IconSize
103         {
104             get { return (double)GetValue(IconSizeProperty); }
105             set { SetValue(IconSizeProperty, value); }
106         }
107         
108     }
109 }
自定義類,繼承button
 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:MF.WPF.CustomControls.RoundButton"                                      
 4                     >
 5     
 6     <SolidColorBrush x:Key="Accent" Color="#0072C6" />
 7     <SolidColorBrush x:Key="ModernButtonBorder" Color="#919191" />
 8     <SolidColorBrush x:Key="ModernButtonTextHover" Color="#d1d1d1" />
 9     <SolidColorBrush x:Key="ModernButtonTextPressed" Color="White" />
10     <SolidColorBrush x:Key="ModernButtonBorderPressed" Color="White" />
11     <SolidColorBrush x:Key="ModernButtonIconForegroundPressed" Color="White" />
12     <Style TargetType="{x:Type local:RoundButton}">
13         <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
14         <Setter Property="HorizontalContentAlignment" Value="Center" />
15         <Setter Property="VerticalContentAlignment" Value="Center" />
16         <Setter Property="Foreground" Value="Black" />
17         <Setter Property="Padding" Value="1" />
18         <Setter Property="Template">
19             <Setter.Value>
20                 <ControlTemplate TargetType="{x:Type local:RoundButton}">
21                     <Grid Width="{TemplateBinding EllipseDiameter}" Height="{TemplateBinding EllipseDiameter}" >
22                         <Ellipse x:Name="ellipse"
23                                      Stroke="{DynamicResource ModernButtonBorder}"
24                                      StrokeThickness="{TemplateBinding EllipseStrokeThickness}"                                    
25                                      VerticalAlignment="Stretch" />
26                         <Path x:Name="icon"
27                                   Data="{TemplateBinding IconData}" 
28                                   Width="{TemplateBinding IconSize}"
29                                   Height="{TemplateBinding IconSize}"
30                                   Fill="{TemplateBinding Foreground}"                                   
31                                   Stretch="Uniform" HorizontalAlignment="Center"
32                                   VerticalAlignment="Center"/>
33                     </Grid>
34                     <ControlTemplate.Triggers>
35                         <Trigger Property="IsMouseOver" Value="True">
36                             <Setter TargetName="ellipse" Property="Stroke" Value="{DynamicResource Accent}" />
37                         </Trigger>
38                         <Trigger Property="IsPressed" Value="True">
39                             <Setter Property="Foreground" Value="{DynamicResource ModernButtonTextPressed}" />
40                             <Setter TargetName="ellipse" Property="Stroke" Value="{DynamicResource ModernButtonBorderPressed}" />
41                             <Setter TargetName="ellipse" Property="Fill" Value="{DynamicResource Accent}" />
42                             <Setter TargetName="icon" Property="Fill" Value="{DynamicResource ModernButtonIconForegroundPressed}" />
43                         </Trigger>
44                         <Trigger Property="IsEnabled" Value="false">
45                             <Setter Property="Foreground" Value="{DynamicResource ModernButtonTextHover}" />
46                             <Setter TargetName="ellipse" Property="Stroke" Value="{DynamicResource ModernButtonTextHover}" />
47                             <Setter TargetName="icon" Property="Fill" Value="{DynamicResource ModernButtonBorder}" />
48                         </Trigger>
49                         <Trigger Property="IsFocused" Value="true">
50                             <Setter TargetName="ellipse" Property="Stroke" Value="{DynamicResource Accent}" />
51                         </Trigger>
52                     </ControlTemplate.Triggers>
53                 </ControlTemplate>
54             </Setter.Value>
55         </Setter>
56     </Style>
57 </ResourceDictionary>
58 
59 自定義button添加樣式

 

 

使用此庫是,記得在自己項目中添加樣式文件 地址:/MF.WPF.CustomControls.RoundButton;component/Themes/Generic.xaml

完整引用

 1 <Application x:Class="MF.WPF.CustomControls.RoundButton.Test.App"
 2              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4              xmlns:local="clr-namespace:MF.WPF.CustomControls.RoundButton.Test"
 5              StartupUri="MainWindow.xaml">
 6     <Application.Resources>
 7         <ResourceDictionary>
 8             <ResourceDictionary.MergedDictionaries>
 9                 <ResourceDictionary Source="/MF.WPF.CustomControls.RoundButton;component/Themes/Generic.xaml"/>                
10             </ResourceDictionary.MergedDictionaries>
11         </ResourceDictionary>
12     </Application.Resources>
13 </Application>

 

關於失量圖標可參考此網站
http://modernuiicons.com/

直接動態庫下載

下載源代和測試用例

 

End


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM