0

Inner Exception Message: Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'

<DataGrid CurrentCellChanged="{Binding CallCommand}" AutoGenerateColumns="False" ItemsSource="{Binding MobileList, UpdateSourceTrigger=PropertyChanged}" SelectionUnit="FullRow" IsReadOnly="True">
    <DataGrid.InputBindings>
        <KeyBinding Key="C" Modifiers="Ctrl" Command="{Binding Path=DataContext.CopyToClipBoardCommand}" CommandParameter="{Binding }" />
    </DataGrid.InputBindings>
    <DataGrid.Columns>
        <!--Column 1-->
        <DataGridTextColumn Binding="{Binding MobileName}" Header="Name" />
        <!--Column 2-->
        <DataGridTextColumn Binding="{Binding MobileOS}" Header="OS" />
    </DataGrid.Columns>
</DataGrid>

My RelayCommand Source Code:

public RelayCommand<KeyboardEventArgs> CallCommand
{
    get
    {
        return new RelayCommand<KeyboardEventArgs>((selectedItem) =>
        {

        });
    }
}

Kindly assist me, how to Bind the RelayCommand to the CurrentCellChanged property in a DataGrid using MVVM approach ?

Kindly Bind the Event Command to CurrentCellChanged Property.

B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
  • Possible duplicate of [WPF calling commands via events](http://stackoverflow.com/questions/1048517/wpf-calling-commands-via-events) – Sinatr Jan 12 '16 at 10:35
  • @Sinatr - In your Link, the Context is same but the issue is different, here I'm facing the Error. Let I know why it throw an error? – B.Balamanigandan Jan 12 '16 at 10:41
  • It throw error because you are trying to bind `ICommand` to event. It's not possible as it is. There are special frameworks making that possible (e.g. MVVMlight, see [here](http://stackoverflow.com/a/5869889/1997232) example). – Sinatr Jan 12 '16 at 10:57
  • How can I link Interaction Trigger for the said property CurrentCellChanged – B.Balamanigandan Jan 12 '16 at 11:08
  • What is interaction trigger? In MVVM you do most of stuff with bindings (see [this](http://stackoverflow.com/q/20080130/1997232)), instead of using command you can bind property (in your case `CurrentCell` perhaps) and perform some action in property setter. – Sinatr Jan 12 '16 at 11:13
  • Yes, I know that. But I need the alternate way that's I preferred this Binding. How can I achieve this. Kindly assist me. – B.Balamanigandan Jan 12 '16 at 11:17
  • "I know it's wrong and impossible, but I want it" - is that what you are saying? See [XY problem](http://meta.stackexchange.com/q/66377/299295), what you need should be possible to do but **not the way you trying to do it**. – Sinatr Jan 12 '16 at 11:24

1 Answers1

1

Use System.Windows.Interactivity.dll for using interaction it is used for binding command to event thus, your MVVM pattern not violate.

At the top in Window/Usercontrol you have to write namespace for using interaction,

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

here,I put some xaml code for it.

 <DataGrid  AutoGenerateColumns="False" 
                   ItemsSource="{Binding MobileList, UpdateSourceTrigger=PropertyChanged}" 
                   SelectionUnit="FullRow" IsReadOnly="True" >

            <DataGrid.InputBindings>
                <KeyBinding Key="C" Modifiers="Ctrl" Command="{Binding Path=DataContext.CopyToClipBoardCommand}" CommandParameter="{Binding }" />
            </DataGrid.InputBindings>

            <i:Interaction.Triggers>
                <i:EventTrigger  EventName="CurrentCellChanged">
                    <i:InvokeCommandAction Command="{Binding Path=DataContext.CallCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>

            <DataGrid.Columns>

                <!--Column 1-->
                <DataGridTextColumn Binding="{Binding MobileName}" Header="Name" />
                <!--Column 2-->
                <DataGridTextColumn Binding="{Binding MobileOS}" Header="OS" />
            </DataGrid.Columns>
        </DataGrid>
Himalaya
  • 81
  • 7