Forum Xamarin.Forms

Call Command of ViewModel from DataTemplate

radkapperadkappe Member ✭✭
edited June 2019 in Xamarin.Forms


I am stuck on a binding problem here.

I created a bindable layout which is inside a control template:

<ContentView x:Name="SettingsMenu" ControlTemplate="{StaticResource HeaderTemplate}" AbsoluteLayout.LayoutBounds="0.5,0.5,1,1" AbsoluteLayout.LayoutFlags="All"> <ScrollView Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> <StackLayout x:Name="SettingsStack" BindableLayout.ItemsSource="{Binding Settings}" BindableLayout.ItemTemplateSelector="{StaticResource SettingsSelectorTemplate}" Orientation="Vertical" Spacing="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" /> </ScrollView> </ContentView>

What I want to do is call a command inside the view model. The call is inside an item template selector as a resource dictionary inside App.xml

<ResourceDictionary> <DataTemplate x:Key="PlaceholderSettingsTemplate"> ### SOME STUFF </DataTemplate> <DataTemplate x:Key="HeaderSettingsTemplate"> ### SOME STUFF <Grid.GestureRecognizers> <TapGestureRecognizer Tapped="ButtonClick" Command="{Binding BindingContext.SettingsTap, Source={x:Reference SettingsPage}}" CommandParameter="{Binding}" /> ########## <--------- WHAT TO USE FOR SOURCE? </Grid.GestureRecognizers> </Grid> </DataTemplate> <data:SettingsSelector x:Key="SettingsSelectorTemplate" Placeholder="{StaticResource PlaceholderSettingsTemplate}" Heading="{StaticResource HeaderSettingsTemplate}" Content="{StaticResource ContentSettingsTemplate}" /> </ResourceDictionary>

Before I moved it inside a resource dictionary in the App.xml file, I simply used the x:Name of the Parent Contentview. But: I can't reference it by name anymore because I moved it into a resource dictionary inside App.xml.

Now, the answer may be trivial but I just can't find a solution.

Any help is appreciated.

Kind regards

Best Answers


  • radkapperadkappe Member ✭✭

    Alright, thanks guys

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    You are welcome, if the reply is helpful, please mark it as an answer, it will help others who have similar issue.

Sign In or Register to comment.