DataTemplate inside DataTemplate

vanKraster 181 Reputation points
2021-12-15T15:14:51.613+00:00

I have a custom control that has a CollectionView with some builtin features like, multiselection with animations, drag & drop and SwipeItems so I created a contentView with a collectionview that has a
<CollectionView.ItemTemplate>
<DataTemplate>

with inside a

 <TemplatedView HorizontalOptions="FillAndExpand" 
                                                           ControlTemplate="{Binding Source={x:Reference this}, Path=CustomDataTemplate}" 
                                                           x:Name="TemplateView"></TemplatedView>

When I call my custom list I use

<ControlTemplate x:Key="ctrlRoot">
<StackLayout Orientation="Vertical">
<Label Grid.Column="0" Text="{TemplateBinding Parent.BindingContext.UniqueId}"></Label>
<Label Grid.Column="0" Text="{TemplateBinding Parent.BindingContext.Text}"></Label>
</StackLayout>
</ControlTemplate>

It works but as you can see I must bind data with TemplateBinding and I want to use the {Binding command... it is possible to achieve this somehow ?
I neet to pass a DataTemplate not a ControlTemplate but I don't know how to refactor this...
Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,253 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. vanKraster 181 Reputation points
    2021-12-15T21:06:32.33+00:00

    I have found a way by myself:

    1. add BindingContext={Binding .} to TemplatedView '
    2. Add BindingContext={TemplateBinding BindingContext} to container (let's say grid or stacklayout) of ControlTemplate
    3. Add x:DataType to the same container as 2
    4. You can now Use {Binding <PropertyName>} with intellisense

    This could be a way....

    0 comments No comments