WPF 自定义图片按钮

原文:WPF 自定义图片按钮

此文档仅仅是一个BaseCode,已做后续查阅

XAML代码:

<Button x:Class="IM.UI.UC.IM_ImageButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Name="loc">
    <Image Name="innerImage" Stretch="None" Source="{Binding Path=ImageSourceEx,ElementName=loc}" />
</Button>

CS代码:

 public IM_ImageButton()
        {
            InitializeComponent();
            FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(IM_ImageButton), new FrameworkPropertyMetadata(typeof(Button)));
            this.IsEnabledChanged += new DependencyPropertyChangedEventHandler(ImageButton_IsEnabledChanged);
        }
        public ImageSource GrayImageSourceEx
        {
            get { return (ImageSource)GetValue(GrayImageSourceExProperty); }
            set { SetValue(GrayImageSourceExProperty, value); }
        }
        public static readonly DependencyProperty GrayImageSourceExProperty =
            DependencyProperty.Register("GrayImageSourceEx", typeof(ImageSource), typeof(IM_ImageButton), new UIPropertyMetadata(null));
        public ImageSource ImageSourceEx
        {
            get { return (ImageSource)GetValue(ImageSourceExProperty); }
            set { SetValue(ImageSourceExProperty, value); }
        }
        public static readonly DependencyProperty ImageSourceExProperty =
            DependencyProperty.Register("ImageSourceEx", typeof(ImageSource), typeof(IM_ImageButton), new UIPropertyMetadata(null));

        //当前按钮显示状态被禁用或启用
        protected void ImageButton_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.IsEnabled && (this.ImageSourceEx != null))
            {
                this.innerImage.Source = this.ImageSourceEx;
            }
            else if (!(this.IsEnabled || (this.GrayImageSourceEx == null)))
            {
                this.innerImage.Source = this.GrayImageSourceEx;
            }

        }

原文地址:https://www.cnblogs.com/lonelyxmas/p/10764056.html

时间: 2024-07-31 11:59:20

WPF 自定义图片按钮的相关文章

Mono自定义图片按钮

首先,我们编写一个MyImageButton类,继承自LinearLayout public class MyPhoneImageButton:LinearLayout { private ImageView mButtonImage = null; private TextView mButtonText = null; public MyPhoneImageButton (Context context) : base (context) { mButtonImage = new Image

WPF自定义button按钮控件

一.前言 程序界面上的按钮多种多样,常用的就这几种:普通按钮.图标按钮.文字按钮.图片文字混合按钮.本文章记录了不同样式类型的按钮实现方法.下面话不多说了,来一起看看详细的介绍吧. 二.固定样式的按钮 固定样式的按钮一般在临时使用时或程序的样式比较固定时才会使用,按钮整体样式不需要做大的改动. 2.1 普通按钮-扁平化风格 先看效果: 定义Button的样式,详见代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2

wpf 自定义Button按钮

创建ButtonEx类 public class ButtonEx : Button { static ButtonEx() { DefaultStyleKeyProperty.OverrideMetadata(typeof(ButtonEx), new FrameworkPropertyMetadata(typeof(ButtonEx))); } /// <summary> /// 按钮类型 /// </summary> public ButtonType ButtonType

自己写一个图片按钮(XAML)

有时需要用三张图片(正常状态,鼠标移上,鼠标按下)来作为一个按钮的样式,虽然这种做法不好,应该用矢量的方式制作样式,但有的时候还是需要这样做的. 每次都修改按钮的样式来实现这个做法,既麻烦又会生成大段的XAML代码,不利于维护,抽出一个自定义图片按钮控件,只需传入三张图片的路径即可使用,显然是更好的做法,下面就演示一下如何编写这个控件,VS2015和Blend2015结合使用. 1. 首先,在VS中新建一个WPF自定义控件库,命名为WpfCustomControlLibrary,系统会自动生成一

图片按钮设计

android自带的按钮简直太挫了,所以项目里面的按钮都是自定义的,具有项目统一的风格,而最常见的按钮,莫过于图片按钮.图片按钮本质不是按钮,它可以是ImageView,TextView等等,下面以自定义图片按钮为例 imageView.setImageResource(R.drawable.btnstyle); 在btnstyle.xml文件中 <?xml version="1.0" encoding="utf-8"?> <selector xm

WPF控件库:图片按钮的封装

需求:很多时候界面上的按钮都需要被贴上图片,一般来说: 1.按钮处于正常状态,按钮具有背景图A 2.鼠标移至按钮上方状态,按钮具有背景图B 3.鼠标点击按钮状态,按钮具有背景图C 4.按钮处于不可用状态,按钮具有背景图D 实现起来,毫无疑问,没什么难度.但是过程还是比较繁琐.这里我将这个过程封装为新的控件类:ImageButton ImageButton中有四个属性(支持绑定),分别对应上面A.B.C.D四个背景图的路径. #region 属性 /// <summary> /// 按钮处于正常

WP8_给图片、按钮设置自定义图片

工程目录下新建文件夹Images,将图片文件复制到Images文件夹里,本文以image1为例 1).在xaml里加背景图片 image图片: <Stretch="Fill" Source="Images/image1.png"  Name="image"/> Button背景图片:<Button Name="button1" >            <Button.Background>

html+css实现自定义图片上传按钮 &#207061;

原文: http://blog.gqylpy.com/gqy/492 置顶:来自一名75后老程序员的武林秘籍--必读(博主推荐) 来,先呈上武林秘籍链接:http://blog.gqylpy.com/gqy/401/ 你好,我是一名极客!一个 75 后的老工程师! 我将花两分钟,表述清楚我让你读这段文字的目的! 如果你看过武侠小说,你可以把这个经历理解为,你失足落入一个山洞遇到了一位垂暮的老者!而这位老者打算传你一套武功秘籍! 没错,我就是这个老者! 干研发 20 多年了!我也年轻过,奋斗过!我

wpf 自定义按钮

一  代码结构       如图所示,采用自定义控件(CustomControl)的方式对Button控件进行封装.其中ImageButton.xaml为默认控件模板,ImageButton.cs为控件的逻辑控制文件,其中包含了ImageButton控件所需要的新的依赖属性,包括图片源属性等. 二 模板代码 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x