Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CommandConverter cannot convert from System.String in WPF

Tags:

c#

wpf

I have 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 receive an error that CommandConverter cannot convert from System.String

Where is my mistake ?

I have another binding to a ListView, like:

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

like image 917
Snake Eyes Avatar asked Apr 12 '14 08:04

Snake Eyes


1 Answers

If you want to use Custom Routed commands, you to use more verbose definition.

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


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

<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>
like image 102
Rohit Vats Avatar answered Oct 23 '22 23:10

Rohit Vats