question

RoniCakar-9263 avatar image
0 Votes"
RoniCakar-9263 asked JessieZhang-2116 commented

DataTemplateSelector: How to access ParentProperty with Bindings

I have a CarouselView which selects a Template while Runtime that includes a CollectionView.

 <ContentPage.Resources> 
  <DataTemplate x:Key="xxTemplate">  
             <CollectionView ItemsSource="{Binding .}"  
                             HeightRequest="{Binding Source={TemplatedParent}, Path=Height}">  
                 <CollectionView.ItemTemplate>  
                     <DataTemplate>  
                         ...  
                     </DataTemplate>  
                 </CollectionView.ItemTemplate>  
             </CollectionView>  
         </DataTemplate>  
      
         <DataTemplate x:Key="yyTemplate">  
             <CollectionView ItemsSource="{Binding .}">  
                 <CollectionView.ItemTemplate>  
                     <DataTemplate>  
                         ...  
                     </DataTemplate>  
                 </CollectionView.ItemTemplate>  
             </CollectionView>  
         </DataTemplate>  
         <util:xyDataTemplateSelector x:Key="xySelector"  
                                              xx="{StaticResource xxTemplate}"  
                                              yy="{StaticResource yyTemplate}" /> 
 </ContentPage.Resources> 



This CarouselView consumes the Templates above:

 <CarouselView ItemsSource="{Binding xy}"  
                                       ItemTemplate="{StaticResource xySelector}"  
                                       x:Name="xyCarouselView">  
                             <CarouselView.ItemsLayout>  
                                 <LinearItemsLayout Orientation="Horizontal"  
                                                    SnapPointsType="MandatorySingle"  
                                                    SnapPointsAlignment="Center"  
                                                     />  
                             </CarouselView.ItemsLayout>  
                         </CarouselView>  

The Problem is that the Height of the CollectionView is not fitted to the CarouselViews Height. I wanted to achieve this with a binding. But it doesn't work.

The critical Line is HeightRequest="{Binding Source={TemplatedParent}, Path=Height}".

Can someone Help me?

Thx in advance.



dotnet-xamarin
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi anonymous user ,what's the code of xyDataTemplateSelector ? If it is convenient for you, could you please post a basic demo to github or onedriver so that we can test on our side?
For how to bind to a templated parent , you can check document : https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/relative-bindings#bind-to-a-templated-parent

0 Votes 0 ·

@JessieZhang-2116
Hey.. thx for your answer.

The DataTemplateSelector is working fine.

I have just the binding problem. And what you refer is that what I have just tried.

0 Votes 0 ·

Hi anonymous user ,have you resolved this problem?

0 Votes 0 ·
Show more comments

0 Answers