MenuItem Command [modified]
-
I have a menuitem which is bound to a command in my vm
<MenuItem Header="_Up" Command="{Binding Path=UpCommand}" />
this works great (canexecute fires and execute fires) except if canexecute returns false then the menuitem is disabled. I'd like to change the menuitem so that the visibility will be hidden/collapsed if CanExecute is returned false. Is there a way to do that (by changing the style) w/o creating a "menuitemviewmodel" or some clunky thing like that.
Don't be overcome by evil, but overcome evil with good
modified on Tuesday, December 29, 2009 3:12 PM
-
I have a menuitem which is bound to a command in my vm
<MenuItem Header="_Up" Command="{Binding Path=UpCommand}" />
this works great (canexecute fires and execute fires) except if canexecute returns false then the menuitem is disabled. I'd like to change the menuitem so that the visibility will be hidden/collapsed if CanExecute is returned false. Is there a way to do that (by changing the style) w/o creating a "menuitemviewmodel" or some clunky thing like that.
Don't be overcome by evil, but overcome evil with good
modified on Tuesday, December 29, 2009 3:12 PM
-
Bind the visibility of the menuitem to its own isenabled property using a BooleanToVisibility converter. That should do it ;)
Yeah that's sort of what i went with except i used the same idea in a trigger
<Style x:Key="MenuItemStyle" TargetType="{x:Type MenuItem}"> <Style.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Visibility" Value="Collapsed" /> </Trigger> </Style.Triggers> </Style> <ContextMenu x:Key="menu"> <MenuItem Header="\_Up" Command="{Binding Path=UpCommand}" Style="{StaticResource MenuItemStyle}" /> </ContextMenu>
It still doesn't sit right with me. I wish i could change it to where it doesn't trigger off of the IsEnabled property. It just seems bad to trigger one property off of another property. I mean, theoretically a developer can trigger Visibility off Enabled, then trigger AllowDrop off Visibility, then trigger Focusable off AllowDrop (you get the point). Not that anyone would ever want to do that, but it does promote that behavior.
Don't be overcome by evil, but overcome evil with good