How to Implement the WPF image button

Source: Internet
Author: User

How to Implement the WPF image button

This example shares the implementation code of the WPF image button for your reference. The details are as follows:

Direct code

Public class ImageButton: System. windows. controls. button {// <summary> /// picture /// </summary> public static readonly DependencyProperty ImageProperty = DependencyProperty. register ("Image", typeof (ImageSource), typeof (ImageButton), new PropertyMetadata (null )); /// <summary> /// Image Width /// </summary> public static readonly DependencyProperty ImageWidthProperty = DependencyProperty. register ("ImageWidth", typeof (double), typeof (ImageButton), new PropertyMetadata (double. naN); // <summary> /// Image Height // </summary> public static readonly DependencyProperty ImageHeightProperty = DependencyProperty. register ("ImageHeight", typeof (double), typeof (ImageButton), new PropertyMetadata (double. naN); // <summary> // constructor // </summary> static ImageButton () {defastylekeyproperty. overrideMetadata (typeof (ImageButton), new System. windows. frameworkPropertyMetadata (typeof (ImageButton);} // <summary> // set the Image // </summary> public ImageSource Image {get {return GetValue (ImageProperty) as ImageSource;} set {SetValue (ImageProperty, value) ;}/// <summary> // image width (attribute) /// </summary> public double ImageWidth {get {return (double) GetValue (ImageWidthProperty);} set {SetValue (ImageWidthProperty, value );}} /// <summary> /// Image Height (attribute) /// </summary> public double ImageHeight {get {return (double) GetValue (ImageHeightProperty );} set {SetValue (ImageHeightProperty, value );}}}

Style code

<Style TargetType="{x:Type xi:ImageButton}"> <Setter Property="Template">  <Setter.Value>  <ControlTemplate TargetType="{x:Type xi:ImageButton}">   <Grid>   <Grid.RowDefinitions>    <RowDefinition Height="*"/>    <RowDefinition Height="Auto"/>   </Grid.RowDefinitions>   <Border x:Name="border" Grid.RowSpan="2" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"     SnapsToDevicePixels="true" CornerRadius="3,3,3,3"/>   <Image Grid.Row="0" Source="{TemplateBinding Image}"     Width="{TemplateBinding ImageWidth}"     Height="{TemplateBinding ImageHeight}"     VerticalAlignment="{TemplateBinding VerticalAlignment}"/>   <ContentPresenter Grid.Row="1" HorizontalAlignment="Center" Margin="{TemplateBinding Padding}"      VerticalAlignment="Center" RecognizesAccessKey="True" />   </Grid>   <ControlTemplate.Triggers>   <Trigger Property="IsPressed" Value="True">    <Setter Property="Foreground" Value="#999999"/>   </Trigger>   </ControlTemplate.Triggers>  </ControlTemplate>  </Setter.Value> </Setter> </Style>

Call an instance

Copy codeThe Code is as follows: <xi: ImageButton Image = ".. /Image/set.png "Content =" add member "ImageHeight =" 52 "ImageWidth =" 52 "Width =" 72 "Height =" 72 "Margin =" 30, 10, 10 "/>

Effect display

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.