Basicaly I am facing very similar problem to the question WPF - how to hide menu item if command's CanExecute is false?, the accepted answer uses a clever workaround to bind to the IsEnabled
instead of CanExecute()
result. Alas IsEnabled
workaround would not work in my case:
I have a collection of KeyBinding
s gathered from the VisualTree during the PreviewGotKeyboardFocus
event from various controls, because of this I can have no assumption whether the original keybinding's element is disabled for some other reason (e.g. IsBeingLoaded
binding) or even if the element is not disabled at all (e.g. Grid
does not disable if cannot execute its KeyBinding
s). How can I bind to the KeyBinding.Command
's current CanExecute()
value?
public class ContextHelperVM : ViewModelBase
{
public ObservableCollection<KeyBinding> ContextEffectiveKeybindings { get; }
// KeyBinding list loading, maintaining, etc. pseudocode
// Keybindings = FocusedElement.TraverseToParentWidnow().GatherKeyBindings();
}
in the ContextHelperView.xaml
I would like to set Opacity to non-executable KeyBindings
<ItemsControl ItemsSource="{Binding ContextEffectiveKeybindings}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Key}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Command.????}" Value="False"><!--how to bind here??-->
<Setter Property="Opacity" Value="0.5">
<DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
How can I fix the code so that the Opacity is correctly set for KeyBinding.Command
s that cannot be executed?