0

I am using System.Windows.Interactivity.dll and Microsoft.Expression.Interaction.dll to do event handling in Viewmodel in my MVVM WPF project. below is the code inside my Xaml:

 <ItemsControl ItemsSource="{Binding Path= HeaderList}" Grid.Row="0" Grid.Column="0" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                  <TextBlock   Text="{Binding Text}" Width="100" HorizontalAlignment="Left" >  
                      <i:Interaction.Triggers>
                          <i:EventTrigger EventName="PreviewMouseLeftButtonDown">
                             <ie:CallMethodAction MethodName="PrevMouseDownEventHandler" TargetObject="{Binding}" />
                          </i:EventTrigger>
                       </i:Interaction.Triggers>
                </TextBlock>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

for this I added namespaces in the same Xaml.

 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
 xmlns:ie="http://schemas.microsoft.com/expression/2010/interactions"

and in my viewmodel I have created a method having PrevMouseDownEventHandler name which is same as that of I mentioned as CallMethod inside EventTigger in the Xaml.

On running my application when I presses mouse button on TextBlock event is generated and look for PrevMouseDownEventHandler method and leave me into following exception:

Could not find method named 'PrevMouseDownEventHandler' on object of type 'string' that matches the expected signature.

this method is as below in my ViewModel.

public void PrevMouseMoveEventHandler(object sender, MouseButtonEventArgs e)
    {
        // Some implementation here;  
    }

I don't have any idea where I am going wrong. Except this all the functionalities inside Viewmodel is working fine for me. what would be possible solution for this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
SST
  • 459
  • 3
  • 20
  • 35

2 Answers2

1

CallMethodAction is a delegate with no parameters and no return value. So the "handler" (really an action trigger) would have to look like this:

public void PrevMouseMoveEventHandler()
{
    // Some implementation here;  
}

Also, you'll need to bind to the View Model (your current binding points to the current item in the ItemsControl). You could do this using RelativeSource binding:

<ie:CallMethodAction MethodName="PrevMouseDownEventHandler" 
    TargetObject="{Binding Path=DataContext,RelativeSource={RelativeSource AncestorType=ItemsControl}" />
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • Doing this way giving me exception Could not find method named 'PrevMouseDownEventHandler' on object of type 'MYVIEWMODEL' that matches the expected signature. – SST Sep 26 '12 at 06:41
0

It is looking for the method on the String object you have bound your Text property to.

Basically your data context has changed from the view model to a property of the View Model.

benPearce
  • 37,735
  • 14
  • 62
  • 96
  • I am newbie in WPF, dont know much about it, So how this issue can be resolved? any code sample ? – SST Sep 26 '12 at 06:47
  • I think you may be able to change your TargetObject binding to use a RelativeSource binding, you want to bind to the parent - [link](http://stackoverflow.com/q/84278/4490) – benPearce Sep 27 '12 at 05:29