CommandConverter cannot convert from System.String to WPF

I have a strange error in WPF using .NET Framework 4.5

<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 get the error CommandConverter cannot convert from System.String

Where is my mistake?

I have another binding with ListView, for example:

 <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.

+7
c # wpf
source share
1 answer

If you want to use Custom Routed commands , you should use a more detailed definition.

Declare the routed command as static in the class, and then use it in XAML using x:Static . You can find the answer here .


For completeness, I post the appropriate code from the answer here:

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

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> 
+9
source share

All Articles