实现一个可任意设置图片的Image Button, 当状态为disable时(IsEnabled=False),图片变灰。
1.首先新建一个类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using System.Globalization;
using System.Windows.Media.Imaging;
namespace WpfAppImageButton.Control
{
class ImageButton : Button
{
private string m_imagepath;
public string ImgPath
{
get { return m_imagepath; }
set { m_imagepath = value; }
}
}
public class Path2UriSource : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string relativePath = value as string;
if (string.IsNullOrEmpty(relativePath))
return null;
relativePath = "pack://application:,,,/" + relativePath;
Uri uri = new Uri(relativePath, UriKind.RelativeOrAbsolute);
FormatConvertedBitmap bitmap = new FormatConvertedBitmap();
bitmap.BeginInit();
bitmap.Source = new BitmapImage(uri);
bitmap.DestinationFormat = System.Windows.Media.PixelFormats.Gray32Float;
bitmap.EndInit();
return bitmap;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
public class Path2ImageBrush : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string relativePath = value as string;
if (string.IsNullOrEmpty(relativePath))
return null;
relativePath = "pack://application:,,,/" + relativePath;
Uri uri = new Uri(relativePath, UriKind.R