wpfbutton按钮禁用_wpf – 当元素被禁用时,按钮上的灰色图像(简单而美丽的方式)...

博客介绍了如何创建一个名为AutoGreyableImage的小类,该类使得菜单项的图像在控件被禁用时能自动变为灰色。通过监听IsEnabled属性并转换图像为灰度,同时处理透明度,实现了禁用状态下的视觉效果。作者提供了详细的代码实现和使用方法。
摘要由CSDN通过智能技术生成

正如托马斯·勒布伦(Thomas Lebrun)在他的职位

How to gray the icon of a MenuItem ?中所说,目前最好的方法可能是创建一个小类AutoGreyableImage,它允许您在禁用控件时具有自动变为灰色的图像。

这是你如何使用它:

Header="Paste">

/>

这里是实现:

///

/// Class used to have an image that is able to be gray when the control is not enabled.

/// Author: Thomas LEBRUN (http://blogs.developpeur.org/tom)

///

public class AutoGreyableImage : Image

{

///

/// Initializes a new instance of the class.

///

static AutoGreyableImage()

{

// Override the metadata of the IsEnabled property.

IsEnabledProperty.OverrideMetadata(typeof(AutoGreyableImage), new FrameworkPropertyMetadata(true, new PropertyChangedCallback(OnAutoGreyScaleImageIsEnabledPropertyChanged)));

}

///

/// Called when [auto grey scale image is enabled property changed].

///

/// The source.

/// The instance containing the event data.

private static void OnAutoGreyScaleImageIsEnabledPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)

{

var autoGreyScaleImg = source as AutoGreyableImage;

var isEnable = Convert.ToBoolean(args.NewValue);

if (autoGreyScaleImg != null)

{

if (!isEnable)

{

// Get the source bitmap

var bitmapImage = new BitmapImage(new Uri(autoGreyScaleImg.Source.ToString()));

// Convert it to Gray

autoGreyScaleImg.Source = new FormatConvertedBitmap(bitmapImage, PixelFormats.Gray32Float, null, 0);

// Create Opacity Mask for greyscale image as FormatConvertedBitmap does not keep transparency info

autoGreyScaleImg.OpacityMask = new ImageBrush(bitmapImage);

}

else

{

// Set the Source property to the original value.

autoGreyScaleImg.Source = ((FormatConvertedBitmap) autoGreyScaleImg.Source).Source;

// Reset the Opcity Mask

autoGreyScaleImg.OpacityMask = null;

}

}

}

}

结果如下:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值