private static void OnIsEnabledPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args) { MenuIconImage menuIconImage = source as MenuIconImage; if (menuIconImage != null) { bool isEnabled = Convert.ToBoolean(args.NewValue); if (!isEnabled) { // Get the source bitmap BitmapImage bitmapImage = new BitmapImage(new Uri(menuIconImage.Source.ToString())); // Convert it to grey menuIconImage.Source = new FormatConvertedBitmap(bitmapImage, PixelFormats.Gray32Float, null, 0); // Create opacity mask for greyscale image as FormatConvertedBitmap does not keep transparency info menuIconImage.OpacityMask = new ImageBrush(bitmapImage); } else { // Set the Source property to the original value menuIconImage.Source = ((FormatConvertedBitmap)menuIconImage.Source).Source; // Reset the opacity mask menuIconImage.OpacityMask = null; } } }
/// <summary> /// Initialises a new instance of the MenuIconExtension class. /// </summary> /// <param name="sourceUri">The source URI of the image to display. Use the absolute path in the project, e. g. "/Images/myicon.png".</param> public MenuIconExtension(string sourceUri) { img = new MenuIconImage(); BitmapImage bmp = new BitmapImage(); bmp.BeginInit(); bmp.UriSource = new Uri("pack://application:,,," + sourceUri, UriKind.Absolute); bmp.EndInit(); img.Source = bmp; img.Width = bmp.PixelWidth; img.Height = bmp.PixelHeight; }