Can't call method from CallMethodAction on EventTrigger of custom TextBox

1.7k views Asked by At

Getting error : 'Could not find method named 'LostFocus' on object of type 'MyType' that matches the expected signature.'

<DataGridTemplateColumn MinWidth="80" Width="1.25*" Header="6">
     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
             <customControlls:NumericTextBox x:Name="cc" 
                     Style="{StaticResource NumericTextboxStyle}" 
                     Text="{Binding AccountsReceivable.OverdueAtTheEndOfTheReportingPeriod, UpdateSourceTrigger=LostFocus}">
                 <interactivity:Interaction.Triggers>
                     <interactivity:EventTrigger EventName="LostFocus" SourceName="cc">
                        <interactions:CallMethodAction TargetObject="{Binding}" MethodName="LostFocus"/>
                     </interactivity:EventTrigger>
                 </interactivity:Interaction.Triggers>
             </customControlls:NumericTextBox>
         </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

And the method in ViewModel which I'm trying to call. I also tried to remove parameters from method, still same error.

public void LostFocus(object sender, EventArgs e){}
2

There are 2 answers

0
Mrg Gek On BEST ANSWER

I got it working. You need to bind TargetObject to DataGrid's DataContext.

<DataGridTemplateColumn MinWidth="80" Width="1.25*" Header="6">
 <DataGridTemplateColumn.CellTemplate>
     <DataTemplate>
         <customControlls:NumericTextBox 
                 Style="{StaticResource NumericTextboxStyle}" 
                 Text="{Binding AccountsReceivable.OverdueAtTheEndOfTheReportingPeriod, UpdateSourceTrigger=LostFocus}">
             <interactivity:Interaction.Triggers>
                 <interactivity:EventTrigger EventName="LostFocus">
                    <interactions:CallMethodAction MethodName="LostFocus" TargetObject="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext}" />
                 </interactivity:EventTrigger>
             </interactivity:Interaction.Triggers>
         </customControlls:NumericTextBox>
     </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>

And the method signature which will be called should be:

public void LostFocus(object sender, RoutedEventArgs e){}
0
Dbl On

I had this issue too and instead wrote my own TriggerAction to get rid of the constraint to have a specific method signature. Mind you that this code would have to be improved, in order to be fully viable (method arguments)

public class InvokeMethodAction : Microsoft.Xaml.Behaviors.TriggerAction<DependencyObject>
{
    public static readonly DependencyProperty TargetObjectProperty = DependencyProperty.Register(
        nameof(TargetObject), typeof(FrameworkElement), typeof(InvokeMethodAction), new PropertyMetadata(default(FrameworkElement)));

    public FrameworkElement TargetObject
    {
        get { return (FrameworkElement) GetValue(TargetObjectProperty); }
        set { SetValue(TargetObjectProperty, value); }
    }

    public static readonly DependencyProperty MethodNameProperty = DependencyProperty.Register(
        nameof(MethodName), typeof(string), typeof(InvokeMethodAction), new PropertyMetadata(default(string)));

    public string MethodName
    {
        get { return (string) GetValue(MethodNameProperty); }
        set { SetValue(MethodNameProperty, value); }
    }

    /// <inheritdoc />
    protected override void Invoke(object parameter)
    {
        if (TargetObject != null && MethodName != null)
        {
            var method = TargetObject.GetType().GetMethod(MethodName);
            if (method != null)
            {
                method.Invoke(TargetObject, null);
            }
        }
    }
}