您好,欢迎来到世旅网。
搜索
您的当前位置:首页WPF——给button添加背景图片

WPF——给button添加背景图片

来源:世旅网
WPF——给button添加背景图⽚

⾸先要肯定,代码:

是正确的。这⾥对Background的设置,就是⽤于添加⼀张背景图⽚。

这样的代码,编译绝对没有问题。但⼀运⾏,马上报错。报的是XML解析的错误。

找来找去,问题在于图⽚ “set.png\" 上。图⽚是存在的,但问题在于它还没有被包含进项⽬中,所以运⾏就出错了。在VS中,将图⽚包含进项⽬,或是将路径改成绝对路径,就⼀切OK。这种错误,不报找不到图⽚,却报XML解析错误,真让⼈丈⼆⾦刚,摸不着头脑。

=============================================================================================只是想做⼀个很简单的图⽚按钮⽽已,不需要那么复杂。

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

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

下⾯是控件重写的⽅案:

⼀个可⾏的解决⽅案为,封装⼀个⽤于图⽚按钮的⾃定义按钮控件,该控件继承⾃Button控件,但是额外增加了⼀些⽤户图⽚绑定的依赖属性,同时在控件的默认外观模板中,通过TemplateBinding的⽅式绑定到依赖属性上,这样在使⽤的时候便可以直接通过绑定的⽅式设置图⽚按钮需要显⽰的图⽚,不再需要修改控件模板。

其实现⽅式如下:

⼀ 代码结构 如图所⽰,

采⽤⾃定义控件(CustomControl)的⽅式对Button控件进⾏封装。其中ImageButton.xaml为默认控件模板,ImageButton.cs为控件的逻辑控制⽂件,其中包含了ImageButton控件所需要的新的依赖属性,包括图⽚源属性等。

⼆ 模板代码

xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:local=\"clr-namespace:CustomControl\">

在模板中,通过TemplateBinding 的⽅式绑定了控件中的⾃定义属性,并默认显⽰给定的图标和⽂字。 然后通过触发器,当⿏标悬停或按下的时候,控制相关图标的显⽰隐藏以及⽂字的背景⾊、前景⾊和边框颜⾊。

三 ⾃定义依赖属性

在ImageButton.cs中定义依赖属性,这些依赖属性包含了图⽚按钮控件的边框、前景⾊、背景⾊,图⽚等属性。在复写的OnApplyTemplate⽅法中,会判断如果某些依赖属性的值为null,则使⽤默认属性。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 }

四 控件的应⽤

应⽤控件的时候,只需要简单的绑定控件的相关属性即可。

HorizontalContentAlignment=\"Center\" VerticalContentAlignment=\"Center\" Background=\"Red\"

Margin=\"5,0,5,0\" ToolTip=\"播放\" Content=\"播放\" Height=\"30\" Width=\"80\"

Icon=\"Images\\BroadcastWhite.png\"

IconMouseOver=\"Images\\BroadcastGray.png\" IconPress=\"Images\\BroadcastBlue.png\" IconHeight=\"24\" IconWidth=\"24\" FontSize=\"16\"

IconContentMargin=\"10,0,0,0\" Foreground=\"#FFFFFF\"

MouseOverForeground=\"Gray\" MouseDownForeground=\"Blue\"/>

让我们看看效果图吧: 正常状态:

⿏标悬停状态:

⿏标按下状态:

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

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- esig.cn 版权所有 湘ICP备2023023988号-3

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务