• WPF——给button添加背景图片


    只是想做一个很简单的图片按钮而已,不需要那么复杂。

    =============================================================================================

    WPF中,如果要想给按钮控件Button加上图片,最直接的做法是修改控件模板,在模板中加入想要的图片,代码如下图所示:

    复制代码









    复制代码

       但是这样做有一个弊端——每次需要用到图片按钮的时候都要去修改模板。因为上面的示例代码中,模板代码过于精简,所以乍看之下似乎这种做法也没有什么不好。但是在实际的应用中,按钮控件的模板往往复杂得多,比如,有很多的Trigger事件,往往需要根据鼠标或按钮的状态来调整控件的图片、字体、背景等状态。因此,如果每次应用图片控件的时候都修改模板,很可能会导致xaml文件的代码量爆炸。
      先给出一个简单的MVVM的绑定方式,供大家参考吧,后面会给出控件重写的方案。
    
    • 1
    • 2

    复制代码

    下面是控件重写的方案:
    一个可行的解决方案为,封装一个用于图片按钮的自定义按钮控件,该控件继承自Button控件,但是额外增加了一些用户图片绑定的依赖属性,同时在控件的默认外观模板中,通过TemplateBinding的方式绑定到依赖属性上,这样在使用的时候便可以直接通过绑定的方式设置图片按钮需要显示的图片,不再需要修改控件模板。

       其实现方式如下:
    
    
    
       一  代码结构
       如图所示,
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    采用自定义控件(CustomControl)的方式对Button控件进行封装。其中ImageButton.xaml为默认控件模板,ImageButton.cs为控件的逻辑控制文件,其中包含了ImageButton控件所需要的新的依赖属性,包括图片源属性等。

       二 模板代码
    
    • 1

    复制代码

      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    复制代码
       在模板中,通过TemplateBinding 的方式绑定了控件中的自定义属性,并默认显示给定的图标和文字。
       然后通过触发器,当鼠标悬停或按下的时候,控制相关图标的显示隐藏以及文字的背景色、前景色和边框颜色。
    
    
       三 自定义依赖属性
       在ImageButton.cs中定义依赖属性,这些依赖属性包含了图片按钮控件的边框、前景色、背景色,图片等属性。在复写的OnApplyTemplate方法中,会判断如果某些依赖属性的值为null,则使用默认属性。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    复制代码
    public class ImageButton : Button
    {
    static ImageButton()
    {
    DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
    }

    public override void OnApplyTemplate()  
    {  
        base.OnApplyTemplate();  
        if (this.MouseOverBackground == null)  
        {  
            this.MouseOverBackground = Background;  
        }  
        if (this.MouseDownBackground == null)  
        {  
            if (this.MouseOverBackground == null)  
            {  
                this.MouseDownBackground = Background;  
            }  
            else  
            {  
                this.MouseDownBackground = MouseOverBackground;  
            }  
        }  
        if (this.MouseOverBorderBrush == null)  
        {  
            this.MouseOverBorderBrush = BorderBrush;  
        }  
        if (this.MouseDownBorderBrush == null)  
        {  
            if (this.MouseOverBorderBrush == null)  
            {  
                this.MouseDownBorderBrush = BorderBrush;  
            }  
            else  
            {  
                this.MouseDownBorderBrush = MouseOverBorderBrush;  
            }  
        }  
        if (this.MouseOverForeground == null)  
        {  
            this.MouseOverForeground = Foreground;  
        }  
        if (this.MouseDownForeground == null)  
        {  
            if (this.MouseOverForeground == null)  
            {  
                this.MouseDownForeground = Foreground;  
            }  
            else  
            {  
                this.MouseDownForeground = this.MouseOverForeground;  
            }  
        }  
    }  
    
    #region Dependency Properties  
    
    ///   
    /// 鼠标移上去的背景颜色  
    ///   
    public static readonly DependencyProperty MouseOverBackgroundProperty  
        = DependencyProperty.Register("MouseOverBackground", typeof(Brush), typeof(ImageButton));  
    
    ///   
    /// 鼠标按下去的背景颜色  
    ///   
    public static readonly DependencyProperty MouseDownBackgroundProperty  
        = DependencyProperty.Register("MouseDownBackground", typeof(Brush), typeof(ImageButton));  
    
    ///   
    /// 鼠标移上去的字体颜色  
    ///   
    public static readonly DependencyProperty MouseOverForegroundProperty  
        = DependencyProperty.Register("MouseOverForeground", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));  
    
    ///   
    /// 鼠标按下去的字体颜色  
    ///   
    public static readonly DependencyProperty MouseDownForegroundProperty  
        = DependencyProperty.Register("MouseDownForeground", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));  
    
    ///   
    /// 鼠标移上去的边框颜色  
    ///   
    public static readonly DependencyProperty MouseOverBorderBrushProperty  
        = DependencyProperty.Register("MouseOverBorderBrush", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));  
    
    ///   
    /// 鼠标按下去的边框颜色  
    ///   
    public static readonly DependencyProperty MouseDownBorderBrushProperty  
        = DependencyProperty.Register("MouseDownBorderBrush", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));  
    
    ///   
    /// 圆角  
    ///   
    public static readonly DependencyProperty CornerRadiusProperty  
        = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(ImageButton), null);  
    
    //图标  
    public static readonly DependencyProperty IconProperty  
        = DependencyProperty.Register("Icon", typeof(ImageSource), typeof(ImageButton), null);  
    
    //鼠标移上去的图标图标  
    public static readonly DependencyProperty IconMouseOverProperty  
        = DependencyProperty.Register("IconMouseOver", typeof(ImageSource), typeof(ImageButton), null);  
    
    //鼠标按下去的图标图标  
    public static readonly DependencyProperty IconPressProperty  
        = DependencyProperty.Register("IconPress", typeof(ImageSource), typeof(ImageButton), null);  
    
    //图标高度  
    public static readonly DependencyProperty IconHeightProperty  
        = DependencyProperty.Register("IconHeight", typeof(double), typeof(ImageButton), new PropertyMetadata(24.0, null));  
    
    //图标宽度  
    public static readonly DependencyProperty IconWidthProperty  
        = DependencyProperty.Register("IconWidth", typeof(double), typeof(ImageButton), new PropertyMetadata(24.0, null));  
    
    //图标和内容的对齐方式  
    public static readonly DependencyProperty IconContentOrientationProperty  
        = DependencyProperty.Register("IconContentOrientation", typeof(Orientation), typeof(ImageButton), new PropertyMetadata(Orientation.Horizontal, null));  
    
    //图标和内容的距离  
    public static readonly DependencyProperty IconContentMarginProperty  
        = DependencyProperty.Register("IconContentMargin", typeof(Thickness), typeof(ImageButton), new PropertyMetadata(new Thickness(0, 0, 0, 0), null));  
    
    #endregion  
    
    #region Property Wrappers  
    
    public Brush MouseOverBackground  
    {  
        get  
        {  
            return (Brush)GetValue(MouseOverBackgroundProperty);  
        }  
        set { SetValue(MouseOverBackgroundProperty, value); }  
    }  
    
    public Brush MouseDownBackground  
    {  
        get  
        {  
            return (Brush)GetValue(MouseDownBackgroundProperty);  
        }  
        set { SetValue(MouseDownBackgroundProperty, value); }  
    }  
    
    public Brush MouseOverForeground  
    {  
        get  
        {  
            return (Brush)GetValue(MouseOverForegroundProperty);  
        }  
        set { SetValue(MouseOverForegroundProperty, value); }  
    }  
    
    public Brush MouseDownForeground  
    {  
        get  
        {  
            return (Brush)GetValue(MouseDownForegroundProperty);  
        }  
        set { SetValue(MouseDownForegroundProperty, value); }  
    }  
    
    public Brush MouseOverBorderBrush  
    {  
        get { return (Brush)GetValue(MouseOverBorderBrushProperty); }  
        set { SetValue(MouseOverBorderBrushProperty, value); }  
    }  
    
    public Brush MouseDownBorderBrush  
    {  
        get { return (Brush)GetValue(MouseDownBorderBrushProperty); }  
        set { SetValue(MouseDownBorderBrushProperty, value); }  
    }  
    
    public CornerRadius CornerRadius  
    {  
        get { return (CornerRadius)GetValue(CornerRadiusProperty); }  
        set { SetValue(CornerRadiusProperty, value); }  
    }  
    
    public ImageSource Icon  
    {  
        get { return (ImageSource)GetValue(IconProperty); }  
        set { SetValue(IconProperty, value); }  
    }  
    
    public ImageSource IconMouseOver  
    {  
        get { return (ImageSource)GetValue(IconMouseOverProperty); }  
        set { SetValue(IconMouseOverProperty, value); }  
    }  
    
    public ImageSource IconPress  
    {  
        get { return (ImageSource)GetValue(IconPressProperty); }  
        set { SetValue(IconPressProperty, value); }  
    }  
    
    public double IconHeight  
    {  
        get { return (double)GetValue(IconHeightProperty); }  
        set { SetValue(IconHeightProperty, value); }  
    }  
    
    public double IconWidth  
    {  
        get { return (double)GetValue(IconWidthProperty); }  
        set { SetValue(IconWidthProperty, value); }  
    }  
    
    public Orientation IconContentOrientation  
    {  
        get { return (Orientation)GetValue(IconContentOrientationProperty); }  
        set { SetValue(IconContentOrientationProperty, value); }  
    }  
    
    public Thickness IconContentMargin  
    {  
        get { return (Thickness)GetValue(IconContentMarginProperty); }  
        set { SetValue(IconContentMarginProperty, value); }  
    }  
    
    #endregion  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223

    }
    复制代码

       四 控件的应用
    
    
       应用控件的时候,只需要简单的绑定控件的相关属性即可。
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    第9章 K8s进阶篇-持久化存储入门
    智能汽车HMI“火了”
    Binder 域
    C++-Cmake指令:cmake_minimum_required
    企业数据图表- FineReport函数计算组成和语法概述
    pytorch使用tensorboardX面板自动生成模型结构图和各类可视化图像
    【SQL】索引失效的11种情况
    【Docker-k8s学习和实战】(七)详解docker容器管理---容器的相关操作
    SpringBoot+Vue 的留守儿童系统的研究与实现,2.0 版本,附数据库、教程
    爱上C语言:操作符详解(下)
  • 原文地址:https://blog.csdn.net/weixin_41883890/article/details/126920023