网站建设资讯

NEWS

网站建设资讯

WPF如何实现自定义按钮Button

这篇文章将为大家详细讲解有关WPF如何实现自定义按钮Button,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联公司于2013年开始,是专业互联网技术服务公司,拥有项目网站设计、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元云阳做网站,已为上家服务,为云阳各地企业和个人服务,联系电话:028-86922220

固定样式的按钮

固定样式的按钮一般在临时使用时或程序的样式比较固定时才会使用,按钮整体样式不需要做大的改动。

普通按钮-扁平化风格

先看效果:

WPF如何实现自定义按钮Button

定义Button的样式,详见代码:


   
   
   
   
   
   
    
     
      
       
      
      
       
        
       
       
        
       
      
     
   
  

引用方法:


  
   

上述代码实现了Button按钮的扁平化样式,如果你想调整颜色风格,通过修改Background的值可实现默认颜色,鼠标经过颜色以及鼠标按下颜色。

图标按钮

先看效果:

WPF如何实现自定义按钮Button

Button样式的代码和扁平化Button差不多,只是把TextBlock控件替换成了Image控件,另外需要设置Button默认的背景色为透明。废话不多说看代码:


   
   
    
     
      
       
      
      
       
        
       
       
        
       
      
     
   
  
 

这里的button1.png需要自己准备图片资源,IsMouseOver和IsPressed的图片资源可自己替换,替换之后能有更丰富的效果呈现。

图标文字混合按钮

效果:

WPF如何实现自定义按钮Button

实现代码:


  
  
    
     
      
       
        
        
       
      
      
       
        
       
      
     
   
  
 

文字按钮和2.3中的图标文字按钮样式差不多,只需要把Image控件去掉就行。

复用性高的按钮

要想实现复用性高的按钮,就必须新建自定义控件。下面这个实例通过自定义控件实现上述所有效果,并且可以随意更改风格。

首先在项目中右键-添加-新建项-自定义控件。

WPF如何实现自定义按钮Button

新建自定义控件之后,添加依赖属性。代码如下:

public class ButtonEx : Button
 {
  static ButtonEx()
  {
   DefaultStyleKeyProperty.OverrideMetadata(typeof(ButtonEx), new FrameworkPropertyMetadata(typeof(ButtonEx)));
  }


  public ButtonType ButtonType
  {
   get { return (ButtonType)GetValue(ButtonTypeProperty); }
   set { SetValue(ButtonTypeProperty, value); }
  }

  public static readonly DependencyProperty ButtonTypeProperty =
   DependencyProperty.Register("ButtonType", typeof(ButtonType), typeof(ButtonEx), new PropertyMetadata(ButtonType.Normal));


  public ImageSource Icon
  {
   get { return (ImageSource)GetValue(IconProperty); }
   set { SetValue(IconProperty, value); }
  }

  public static readonly DependencyProperty IconProperty =
   DependencyProperty.Register("Icon", typeof(ImageSource), typeof(ButtonEx), new PropertyMetadata(null));


  public CornerRadius CornerRadius
  {
   get { return (CornerRadius)GetValue(CornerRadiusProperty); }
   set { SetValue(CornerRadiusProperty, value); }
  }

  public static readonly DependencyProperty CornerRadiusProperty =
   DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(ButtonEx), new PropertyMetadata(new CornerRadius(0)));


  public Brush MouseOverForeground
  {
   get { return (Brush)GetValue(MouseOverForegroundProperty); }
   set { SetValue(MouseOverForegroundProperty, value); }
  }

  public static readonly DependencyProperty MouseOverForegroundProperty =
   DependencyProperty.Register("MouseOverForeground", typeof(Brush), typeof(ButtonEx), new PropertyMetadata());


  public Brush MousePressedForeground
  {
   get { return (Brush)GetValue(MousePressedForegroundProperty); }
   set { SetValue(MousePressedForegroundProperty, value); }
  }

  public static readonly DependencyProperty MousePressedForegroundProperty =
   DependencyProperty.Register("MousePressedForeground", typeof(Brush), typeof(ButtonEx), new PropertyMetadata());


  public Brush MouseOverBorderbrush
  {
   get { return (Brush)GetValue(MouseOverBorderbrushProperty); }
   set { SetValue(MouseOverBorderbrushProperty, value); }
  }

  public static readonly DependencyProperty MouseOverBorderbrushProperty =
   DependencyProperty.Register("MouseOverBorderbrush", typeof(Brush), typeof(ButtonEx), new PropertyMetadata());


  public Brush MouseOverBackground
  {
   get { return (Brush)GetValue(MouseOverBackgroundProperty); }
   set { SetValue(MouseOverBackgroundProperty, value); }
  }

  public static readonly DependencyProperty MouseOverBackgroundProperty =
   DependencyProperty.Register("MouseOverBackground", typeof(Brush), typeof(ButtonEx), new PropertyMetadata());


  public Brush MousePressedBackground
  {
   get { return (Brush)GetValue(MousePressedBackgroundProperty); }
   set { SetValue(MousePressedBackgroundProperty, value); }
  }

  public static readonly DependencyProperty MousePressedBackgroundProperty =
   DependencyProperty.Register("MousePressedBackground", typeof(Brush), typeof(ButtonEx), new PropertyMetadata());
 }

 public enum ButtonType
 {
  Normal,
  Icon,
  Text,
  IconText
 }

为不同类型按钮设置样式,代码如下:


  
   
    
    
    
    
    
    
    
    
    
     
      
       
        
       
       
        
         
         
         
        
        
         
         
         
        
       
      
     
    
   
   
    
    
     
      
       
        
       
       
        
         
        
        
         
        
       
      
     
    
   
   
    
    
    
    
    
     
      
       
       
        
         
        
        
         
        
       
      
     
    
   
   
    
    
    
    
    
     
      
       
        
         
         
        
       
       
        
         
        
        
         
        
       
      
     
    
   
   
 

然后就可以引用该控件了:


  
   
   
   
   
  
 

效果如下:

WPF如何实现自定义按钮Button

至此已经完成Button控件的扩展功能,如果想要添加动画或者设置图标的位置和边距等,可以自己另外添加依赖属性来扩展。

关于“WPF如何实现自定义按钮Button”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


文章题目:WPF如何实现自定义按钮Button
文章地址:http://cdweb.net/article/ijhpss.html