C# 类型引用找不到类型命名命令
我想在我的应用程序中使用自定义的C# 类型引用找不到类型命名命令,c#,wpf,xaml,command,C#,Wpf,Xaml,Command,我想在我的应用程序中使用自定义的命令s进行响应。 因此,我遵循上的说明,为我的命令创建了一个静态类: namespace MyNamespace { public static class Commands { public static readonly RoutedUICommand Create = new RoutedUICommand( "Create Thing", nameof(Create), typeof(
命令
s进行响应。
因此,我遵循上的说明,为我的命令创建了一个静态类:
namespace MyNamespace {
public static class Commands {
public static readonly RoutedUICommand Create = new RoutedUICommand(
"Create Thing", nameof(Create),
typeof(MyControl)
);
}
}
然后我尝试在我的UserControl上使用它:
<UserControl x:Class="MyNamespace.MyControl"
...boilerplate...
xmlns:local="clr-namespace:MyNamespace">
<UserControl.CommandBindings>
<CommandBinding Command="local:Commands.Create"
CanExecute="CanCreateThing"
Executed="CreateThing"/>
</UserControl.CommandBindings>
...the control's contents...
</UserControl>
这一个在绑定的命令=“…”
属性中
Invalid value for property 'Command': 'Microsoft.VisualStudio.DesignTools.Xaml.LanguageService.Semantics.XmlValue'
更新
但是,在排除错误后,带有这些命令的菜单项仍将灰显。相关代码:
<TreeView ...>
<ContextMenu>
<MenuItem Command="{x:Static local:Commands.Create}"/>
</ContextMenu>
...
</TreeView>
在的帮助下,找到了一个修复程序:
首先,我必须将local:Commands.Create
的所有实例替换为{x:Static local:Commands.Create}
。但是,菜单项仍然是灰色的
因此,在每个菜单项中,我添加了一个引用祖先ContextMenu
的CommandTarget
:
<MenuItem Command="{x:Static local:Commands.CreateTrigger}"
CommandTarget="{Binding PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>
这使得菜单项可以点击。在绑定中尝试
Command=“{x:static local:Commands.Create}”
。@Mathew请查看编辑。再次检查DeleteItem命令的CanExecute是否实际指向CanCreateThing方法。@E-Bat发出的声音是DeleteItem
是另一个命令,我的意思是编写Create
。并且它的CanExecute
是正确绑定的,如上面所示。固定问题。
//...
private void CanCreateThing(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = true;
}
//...
<MenuItem Command="{x:Static local:Commands.CreateTrigger}"
CommandTarget="{Binding PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>