How do I remove common bound event handlers in Maui Datagrid Template column?

1 Answer 704 Views
DataGrid
David
Top achievements
Rank 1
Iron
David asked on 10 Oct 2022, 01:32 PM
In a Template Column whose data template contains a Button, each such button bound (by "+=") to a common "Clicked" handler, I need to remove those bindings (by matching "-=") when the containing transient page is popped. How do I do that, seeing that Telerik's Maui DataGrid has neither a rows collection (although the concept of a row is mentioned in the API) nor a cells collection for a given column (and I truly wish it had both!)?
David
Top achievements
Rank 1
Iron
commented on 10 Oct 2022, 05:48 PM

Looks reasonable. Will try. Thank you.

1 Answer, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 10 Oct 2022, 02:49 PM

Hello David,

This is why using event handlers in DataTemplate is a bad idea, it not only breaks MVVM, but it can lead to memory leaks due to unsubscribed handlers.

You should use Command instead; it can safely be data bound to the Button.Command property. The Command delegate is where you invoke the logic that you currently have in the event handler.

Please see Commanding - .NET MAUI | Microsoft Learn

Here's a very simple example:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.MainPage"
             x:Name="MyPage">

    <Button Command="{Binding MyClickCommand, Source={x:Reference MyPage}}"/>

</ContentPage>

public partial class MainPage : ContentPage
{
public MainPage()
 {
	InitializeComponent();
        MyClickCommand = new Command(ExecuteMyClick);
    }

    public Command MyClickCommand { get; set; }

    private void ExecuteMyClick()
 {
        // Use this instead of Clicked event handler
 }
}

Custom Event Lifecycle Implementation

If you cannot use a Command for some reason, you'll need to look into the event lifecycle for Micrsoft MAUI view components (for example, some view elements have a Loaded and Unloaded event). Ultimately, this is outside of the control of the Telerik controls or APIs and is fundamentally unrelated because we don't manage events of child views that you define.

You can also create a custom control for that template and use the OnBindingContextChanged method override. Here's an example where I do this for a custom ListView ItemTemplate => https://github.com/LanceMcCarthy/CustomXamarinDemos/tree/main/src/AsyncTemplateCellDemo 

You can subclass a Grid and use the same approach, when the cell is being offloaded, the BindingContext will set null

when the BindingContext is null, that means the cell is being recycled by the UI virtualization mechanism:

protected override void OnBindingContextChanged()
{
    if (this.BindingContext == null)
    {
        // Unsubscribe event handlers
    }
    else
    {
        // subscribe event handlers
    }

    base.OnBindingContextChanged();
}

Caution: this is not official custom development guidance or consulting and is thus unsupported by Telerik Support. Please move forward with any custom approach with caution and very thorough testing. These are only suggestions for consideration. If you would like to discuss this with Microsoft, please visit dotnet-maui - Microsoft Q&A.

Regards,
Lance | Manager Technical Support
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
DataGrid
Asked by
David
Top achievements
Rank 1
Iron
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or