How to pass method to EventCallback in Blazor Code Behind?

Viewed 2666

When using Blazor (.Net Core v3+) Code Behind I can't pass a method name to a component with an event callback parameter. However when using @code { } in the razor component, I can pass the method name to the component's event callback.

Compiler Error when using code behind, see below:

Argument 2: Cannot convert from method group to EventCallback.

@page "/localized-strings"

@inherits LocalizedStringTypesBase

<SfGrid DataSource="@MyDataSource">
    <!-- Argument 2: Error Here for RowSelected="OnDataGridRowSelect"  -->
    <GridEvents RowSelected="OnDataGridRowSelect" TValue="LocalizedStringType"></GridEvents>
</SfGrid>

public class LocalizedStringTypesBase : ComponentBase
{
    public void OnDataGridRowSelect(RowSelectEventArgs<LocalizedStringType> args)
    {
        // not implemented yet
    }
}

This error goes away with the @code { } block, see below.

@page "/localized-strings"

<SfGrid DataSource="@MyDataSource">
    <!-- No Error Here for RowSelected="OnDataGridRowSelect"  -->
    <GridEvents RowSelected="OnDataGridRowSelect" TValue="LocalizedStringType"></GridEvents>
</SfGrid>


@code {

    public void OnDataGridRowSelect(RowSelectEventArgs<LocalizedStringType> args)
    {
    // not implemented yet
    }
}
0 Answers
Related