Customizing a Button control
The button controls are simple, and we sometimes encapsulate the styles in the project, adding dependency properties to unify them.
Here is an example of how the simple icon control is set
1. UserControl Interface Style
<usercontrol x:class= "Wpfapplication12.iconbutton" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/p Resentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:mc=" Http://schemas . openxmlformats.org/markup-compatibility/2006 "xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 " mc:ignorable= "D" d:designheight= "d:designwidth=" loaded= "iconbutton_onloaded" > & Lt usercontrol.resources> <style targettype= "button" > <setter property= "Template" > <Setter.Value> <controltemplate targettype= "button" > <grid& Gt <rectangle x:name= "T_rectangle" height= "width=" > <Rectangle.Fill> <imagebrush imagesource= "{Binding Imagessource}" ></ImageBrush> </Rectangle.Fill> </Rectangle> &L T ContentPresenter horizontalalignment= "{TemplateBinding horizontalalignment}" verticalalignment= "{TemplateBinding VerticalAlignment} "></ContentPresenter> </Grid> <controlte Mplate. triggers> <trigger property= "IsMouseOver" value= "True" > <setter targetname= "T_rectangle" property= "Height" value= "></Setter> <se" Tter targetname= "T_rectangle" property= "Width" value= "></Setter>" </Trigger> <trigger property= "ispressed" value= "True" > <setter Targ Etname= "T_rectangle" property= "Height" value= "></Setter> <setter targetname= "T_rectangle" property= "Width "value=" ></Setter> </Trigger> </controltempla Te. triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </UserControl.Resources> <Grid> <button click= "Buttonbase_onclick" >< ;/button> </Grid></UserControl>
2, background settings, my side only added a picture path and event delegation. Other self-Add.
Public partial class Iconbutton:usercontrol {public Iconbutton () {InitializeComponent ( ); } public ImageSource Imagessource {get {return (ImageSource) GetValue (imagessourceproperty);} set {SetValue (Imagessourceproperty, value);} } public static readonly DependencyProperty Imagessourceproperty = Dependencyproperty.register ("Imagessource", typeof (ImageSource), typeof (Iconbutton)); private void Iconbutton_onloaded (object sender, RoutedEventArgs e) {var data = new Iconbuttonmodel () {imagessource = Imagessource}; This. DataContext = data; public delegate void Clickeventargs (object sender, RoutedEventArgs e); public event Clickeventargs Click; private void Buttonbase_onclick (object sender, RoutedEventArgs e) {if (Click! = null) { Click (sender, e); }}} public class Iconbuttonmodel {public ImageSource imagessource {get; set;} }
WPF Custom Iconbutton