The one that got away [Simple workarounds for a visual problem when toggling a ContextMenu MenuItem’s IsEnabled property directly]


This blog has moved to a new location and comments have been disabled.

All old posts, new posts, and comments can be found on The blog of dlaa.me.

See you there!

Comments (5)

  1. jimmccurdy says:

    I discovered this problem myself last week and came up with yet another simple solution.

    Whenever you set menuitem.IsEnabled, call VisualStateManager.GoToState() to enter the correct visual state.  I also dim the menu item’s icon if it is disabled:

    menuitem.IsEnabled = enabled;

    VisualStateManager.GoToState(menuitem, (enabled ? "Normal" : "Disabled"), true);

    if (menuitem.Icon is Image)

    (menuitem.Icon as Image).Opacity = (enabled ? 1.0 : 0.5);

  2. Delay says:

    jimmccurdy,

    Good one! Not everyone remembers that it’s possible to change a control’s visual states from outside the control – myself included sometimes. ;)

    The Icon Opacity change you describe makes total sense to me, but I didn’t do that by default because it’s not consistent with the WPF ContextMenu’s default behavior.

    Thanks for sharing another great workaround!

  3. Florin says:

    Hey David. Nice post. I used the info related to the IsEnabled workaround and applied it in my custom control. Blog post here: technologywanderer.wordpress.com/…/creating-a-silverlight-4-filemenu-control-from-a-combobox

  4. Delay says:

    Florin,

    Very cool – thanks for sharing!

  5. snelldl says:

    jimmccurdy:

    Wicked great code! I was tearing my hair out, wondering what I was doing wrong. I tried the bounce, but it kept crashing my app the second time it got called. But with your code, problem solved! Now I can go home for the day.