.NET Maui Access underlying data model in collectionview itemtemplate

159 Views Asked by At

Is there any way, to access the underlying object (item) when using contentview as itemtemplate, or when the contentview is used this way, it is just a tool to display bound properties from particular object ?

Thanks for answers

Hello everyone, I have this setup on contentpage:

<CollectionView x:Name="TheCollection" x:DataType="VMMonkeysViewModel"                                        ItemsSource="{Binding MonkeyList}" SelectionMode="Single" SelectionChanged="MonkeyCollection_SelectionChanged">
                            <CollectionView.ItemTemplate>
                            <DataTemplate x:DataType="model:MMonkeyDataModel">
                                <cview:MonkeyView/>
                            </DataTemplate>
                            </CollectionView.ItemTemplate>
</CollectionView>  

cview:MonkeyView is a contentview with bindings to MMonkeyDataModel, which has some buttons in it, the issue is, that in MonkeyView code-behind, I cant get access to underlying data model (MMonkeyDataModel) to use it in button commands (the contentview's bindingContext is always null in code behind).

Edit:

To be more precise. MonkeyView has this code inside a method:

_CurrentMonkey = this.BindingContext as MMonkeyDataModel;
        if (_CurrentMonkey != null)
        {
         // do some stuff with _CurrentMonkey
        }

This.BindingContext returns always null (even in constructor).

0

There are 0 best solutions below