WPF: Is it standard that when a menuitem is disabled the icon is not greyed out? WPF: Is it standard that when a menuitem is disabled the icon is not greyed out? wpf wpf

WPF: Is it standard that when a menuitem is disabled the icon is not greyed out?


Found Jobi's answer helpful. Here's another way to accomplish the same thing using an Image Style and the MenuItem.Icon:

<MenuItem Header="Add ..." Command="{Binding AddCommand}" >   <MenuItem.Icon>      <Image Source="{StaticResource AddImage}" Style="{StaticResource EnableDisableImageStyle}"/>   </MenuItem.Icon></MenuItem>

And the Style:

<Style x:Key="EnableDisableImageStyle" TargetType="{x:Type Image}">    <Style.Triggers>        <Trigger Property="IsEnabled" Value="False">            <Setter Property="Opacity" Value="0.75"/>            <Setter Property="BitmapEffect">                <Setter.Value>                    <BlurBitmapEffect Radius="2.0" KernelType="Gaussian"/>                </Setter.Value>            </Setter>        </Trigger>    </Style.Triggers></Style>


Yes it is totally up to you. Because you have provided an Icon file. So you need to create Style.Trigger on MenuItem to give disabled effect on that. Either do a Opacity =0.5 or switch image to a different .ico image while IsEnabled=False in the template


Actually I had the same problem and and I could not find a satisfying answer to this question. Changing the opacity is quite simple, but what if you want to change the complete appearance of an Icon for a MenuItem. It took me two days to find a simple solution. Since I am pretty new to WPF I had to read through different concepts and Attached Properties was one that got me to my solution. Here is my code for changing the Icon depending on enabled/disabled MenuItem:

Here is the code, which defines the Attached Properties used by the style setter:

    public static class AltImageProvider    {        public static string GetAltImage(DependencyObject obj)        {            return (string)obj.GetValue(AltImageProperty);        }        public static void SetAltImage(DependencyObject obj, string value)        {             obj.SetValue(AltImageProperty, value);        }        public static readonly DependencyProperty AltImageProperty =                DependencyProperty.RegisterAttached("AltImage",typeof(string),typeof(AltImageProvider));        public static string GetDefImage(DependencyObject obj)        {            return (string)obj.GetValue(DefImageProperty);        }        public static void SetDefImage(DependencyObject obj, string value)        {            obj.SetValue(DefImageProperty, value);        }        public static readonly DependencyProperty DefImageProperty =              DependencyProperty.RegisterAttached("DefImage",typeof(string),typeof(AltImageProvider));    }

Here is the code defining a style for Images like the Icons, that uses the Attached Properties (DefImage and AltImage):

    <Style x:Key="ImageDisableAltImageStyle" TargetType="Image">        <Setter Property="Image.Source" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(local:AltImageProvider.DefImage)}"/>        <Style.Triggers>            <Trigger Property="IsEnabled" Value="False">                <Setter Property="Image.Source" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(local:AltImageProvider.AltImage)}"/>            </Trigger>        </Style.Triggers>    </Style>

Here is how to set the style and Icons for a MenuItem:

    <MenuItem.Icon>        <Image Style="{StaticResource ImageDisableAltImageStyle}" local:AltImageProvider.DefImage="/Icon/play_green.ico" local:AltImageProvider.AltImage="/Icon/play_grey.ico"/>    </MenuItem.Icon>