views:

88

answers:

2

By default menu items become disabled when its command cannot be executed (CanExecute = false). What is the easiest way to make the menu item visible/collapsed based on the CanExecute method?

Thanks

for your convenience here's the solution: Bind the visibility property to the IsEnabled property using "Boolean to Visibility" converter.

A: 

I don't know if this is the easiest way, but you can always create a property which returns the CanExecute() and then bind the Visibility of your element to this property, using a IValueConverter to convert the boolean to Visibility.

Roel
+2  A: 

You can simply bind Visibility to IsEnabled (set to false on CanExecute == false). You still would need an IValueConverter to convert the bool to visible/collapsed.

    public class BooleanToCollapsedVisibilityConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //reverse conversion (false=>Visible, true=>collapsed) on any given parameter
            bool input = (null == parameter) ? (bool)value : !((bool)value);
            return (input) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
MrDosu
Duh! Of course. Thanks a lot.
Gustavo Cavalcanti