基本来说,我遇到的问题与WPF - how to hide menu item if command's CanExecute is false?非常相似,被接受的答案使用了一种巧妙的解决方法来绑定到IsEnabled
而不是CanExecute()
的结果。 las IsEnabled
解决方法在我的情况下不起作用:
在KeyBinding
事件期间,我从各种控件收集了PreviewGotKeyboardFocus
的集合,因此,我无法确定是否由于其他原因(例如{{ 1}}绑定),甚至根本不禁用该元素(例如,IsBeingLoaded
如果无法执行其Grid
也不会禁用)。如何绑定到KeyBinding
的当前KeyBinding.Command
值?
CanExecute()
在public class ContextHelperVM : ViewModelBase
{
public ObservableCollection<KeyBinding> ContextEffectiveKeybindings { get; }
// KeyBinding list loading,maintaining,etc. pseudocode
// Keybindings = FocusedElement.TraverseToParentWidnow().GatherKeyBindings();
}
中,我想将不透明度设置为不可执行的ContextHelperView.xaml
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>
s正确设置不透明度?