WPF图片按钮的实现

Posted SFlyers

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF图片按钮的实现相关的知识,希望对你有一定的参考价值。

直接代码

 1  public class ImageButton : System.Windows.Controls.Button
 2     {
 3 
 4         /// <summary>
 5         /// 图片
 6         /// </summary>
 7         public static readonly DependencyProperty ImageProperty = DependencyProperty.Register("Image", typeof(ImageSource), typeof(ImageButton),
 8             new PropertyMetadata(null));
 9 
10         /// <summary>
11         /// 图片的宽度
12         /// </summary>
13         public static readonly DependencyProperty ImageWidthProperty = DependencyProperty.Register("ImageWidth", typeof(double), typeof(ImageButton),
14             new PropertyMetadata(double.NaN));
15 
16         /// <summary>
17         /// 图片的高度
18         /// </summary>
19         public static readonly DependencyProperty ImageHeightProperty = DependencyProperty.Register("ImageHeight", typeof(double), typeof(ImageButton),
20             new PropertyMetadata(double.NaN));
21 
22         /// <summary>
23         /// 构造函数
24         /// </summary>
25         static ImageButton()
26         {
27             DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), 
28                 new System.Windows.FrameworkPropertyMetadata(typeof(ImageButton)));
29         }
30 
31         /// <summary>
32         /// 设置图片
33         /// </summary>
34         public ImageSource Image
35         {
36             get
37             {
38                 return GetValue(ImageProperty) as ImageSource;
39             }
40             set
41             {
42                 SetValue(ImageProperty, value);
43             }
44         }
45 
46         /// <summary>
47         /// 图片宽度(属性)
48         /// </summary>
49         public double ImageWidth
50         {
51             get
52             {
53                 return (double)GetValue(ImageWidthProperty);
54             }
55             set
56             {
57                 SetValue(ImageWidthProperty, value);
58             }
59         }
60 
61         /// <summary>
62         /// 图片高度(属性)
63         /// </summary>
64         public double ImageHeight
65         {
66             get
67             {
68                 return (double)GetValue(ImageHeightProperty);
69             }
70             set
71             {
72                 SetValue(ImageHeightProperty, value);
73             }
74         }
75 
76     }

样式代码

 1     <Style TargetType="{x:Type xi:ImageButton}">
 2         <Setter Property="Template">
 3             <Setter.Value>
 4                 <ControlTemplate TargetType="{x:Type xi:ImageButton}">
 5                     <Grid>
 6                         <Grid.RowDefinitions>
 7                             <RowDefinition Height="*"/>
 8                             <RowDefinition Height="Auto"/>
 9                         </Grid.RowDefinitions>
10                         <Border x:Name="border" Grid.RowSpan="2" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" 
11                                 SnapsToDevicePixels="true" CornerRadius="3,3,3,3"/>
12                         <Image Grid.Row="0" Source="{TemplateBinding Image}"
13                                    Width="{TemplateBinding ImageWidth}"
14                                    Height="{TemplateBinding ImageHeight}"
15                                    VerticalAlignment="{TemplateBinding VerticalAlignment}"/>
16                         <ContentPresenter Grid.Row="1" HorizontalAlignment="Center" Margin="{TemplateBinding Padding}"   
17                                     VerticalAlignment="Center" RecognizesAccessKey="True" />
18                     </Grid>
19                     <ControlTemplate.Triggers>
20                         <Trigger Property="IsPressed" Value="True">
21                             <Setter Property="Foreground" Value="#999999"/>
22                         </Trigger>
23                     </ControlTemplate.Triggers>
24 
25                 </ControlTemplate>
26             </Setter.Value>
27         </Setter>
28     </Style>

调用实例

1     <xi:ImageButton Image="../Image/设置.png" Content="新增会员" ImageHeight="52" ImageWidth="52" Width="72" Height="72" Margin="30,10,10,10"/>

效果展示

本文原创出处:http://www.cnblogs.com/SFlyers/

欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利!

以上是关于WPF图片按钮的实现的主要内容,如果未能解决你的问题,请参考以下文章

WPF 自定义图片按钮

WPF中怎么在工具栏上做既有图片又有文本的按钮?

WPF 给Button按钮加小图标图片Image

WPF 给Button按钮加小图标图片Image

WPF自定义button按钮控件

JavaFX自定义窗口标题栏