CommandConverter 无法从 WPF 中的 System.String 转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23028299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CommandConverter cannot convert from System.String in WPF
提问by Snake Eyes
I have strange error in WPF using .NET Framework 4.5
我在使用 .NET Framework 4.5 的 WPF 中有奇怪的错误
<Window.CommandBindings>
<CommandBinding Command="ImportExcelCmd" CanExecute="ImportExcelCmd_CanExecute" Executed="ImportExcelCmd_Executed"></CommandBinding>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="I" Modifiers="Control" Command="ImportExcelCmd"></KeyBinding>
</Window.InputBindings>
I receive an error that CommandConverter cannot convert from System.String
我收到一个错误 CommandConverter cannot convert from System.String
Where is my mistake ?
我的错误在哪里?
I have another binding to a ListView, like:
我有另一个绑定到 ListView,例如:
<ListView.CommandBindings>
<CommandBinding Command="Delete" CanExecute="Delete_CanExecute" Executed="Delete_Executed"></CommandBinding>
</ListView.CommandBindings>
<ListView.InputBindings>
<KeyBinding Key="Delete" Command="Delete"></KeyBinding>
</ListView.InputBindings>
and it works.
它有效。
回答by Rohit Vats
If you want to use Custom Routed commands, you to use more verbose definition.
如果要使用Custom Routed commands,则使用更详细的定义。
Declare the routed command as staticin class and then use it in XAML using x:Static.
You can refer to the answer here.
static在类中声明路由命令,然后在 XAML 中使用x:Static. 你可以参考这里的答案。
For the sake of completeness of the answer, I am posting the relevant code from the answer here:
为了答案的完整性,我在此处发布答案中的相关代码:
namespace MyApp.Commands
{
public class MyApplicationCommands
{
public static RoutedUICommand ImportExcelCmd
= new RoutedUICommand("Import excel command",
"ImportExcelCmd",
typeof(MyApplicationCommands));
}
}
XAML
XAML
<Window x:Class="..."
...
xmlns:commands="clr-namespace:MyApp.Commands">
...
<Window.CommandBindings>
<CommandBinding
Command="{x:Static commands:MyApplicationCommands.ImportExcelCmd}"
CanExecute="ImportExcelCmd_CanExecute"
Executed="ImportExcelCmd_Executed" />
</Window.CommandBindings>

