Can you subclass a Maui element?

Lloyd Sheen 1,476 Reputation points
2023-05-04T18:51:40.3466667+00:00

I am in the process of getting rid of 3rd party controls. One of the things that is needed is to make a collectionview control that takes a list (just a list) and a grouping property and does the background work to make the grouping list so that I do not have to change much in the existing XAML.

My first attempt was to make an element that was just subclassed from the CollectionView (just a .cs file) -

public partial class MyCollectionView : CollectionView

Now while that works without my changes it has a problem when I want to use a bindableproperty to pass the list to be converted to a grouped list. I can create the BindableProperty and bind the ItemsSource of the CollectionView to the list and it will show the list (not grouped). Now I want to know when the list is sent to my property so I can convert that list to a grouped list. I would use the PropertyChanged event to do that but I can find no way to set an event handler to do this. First off I have created a constructor to add the event handler but when I do I get -

System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'

Now I have about 30 other tests but I am thinking to ask this before I keep going on this. So is this approach (subclassing a control) a valid way to basically add a property that when set will massage the list into a grouped list and set the ItemsSource of the control.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,231 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lloyd Sheen 1,476 Reputation points
    2023-05-04T22:24:36.4366667+00:00

    Ok the solution to the problem was that I did not know about the propertychanged event that could be defined in the BindableProperty.Create. Once I had that I can capture the ItemSource and convert it to the grouped list by using another bindableproperty that contains the property name of the grouping. With a little reflection and some linq I can create the new ItemsSource and set it in code and it shows correctly.

    Rob thanks for your help

    0 comments No comments