CommandConverter无法从WPF中的System.String转换

Sna*_*yes 9 c# wpf

我使用.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>
Run Code Online (Sandbox Code Playgroud)

我收到一个错误 CommandConverter cannot convert from System.String

我的错误在哪里?

我有另一个绑定到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>
Run Code Online (Sandbox Code Playgroud)

它的工作原理.

Roh*_*ats 11

如果要使用Custom Routed commands,则使用更详细的定义.

static在类中声明routed命令,然后在XAML中使用它x:Static.你可以参考这里的答案.


为了完整答案,我发布了答案的相关代码:

namespace MyApp.Commands
{
    public class MyApplicationCommands
    {
        public static RoutedUICommand ImportExcelCmd 
                            = new RoutedUICommand("Import excel command", 
                                                  "ImportExcelCmd", 
                                                  typeof(MyApplicationCommands));
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)