Home > Software design >  Calling the parent command in the child in Xaml in Xamarin.Forms
Calling the parent command in the child in Xaml in Xamarin.Forms

Time:03-29

I have an expander and wants to call its Command in its child (There is another controller which overrides the touch gesture, that's why I need to again override it and call the parent's touch event)

So I have added a grid and I want that when I touch the grid, the Expander's command (which is ExpanderTappedCommand) should be called (with its parameter)

Here is my code but It does not work

<controls:ExtendedExpander
        Command="{Binding Path=BindingContext.ExpanderTappedCommand, Source={x:Reference this}}"
        CommandParameter="{Binding .}"
        AdditionalCommandParameter="{Binding Source={x:Reference stackLayout}}">
     <xct:Expander.Header>

         ......

      <Grid.GestureRecognizers>
          <TapGestureRecognizer                                             
            Command="{Binding Source={RelativeSource AncestorType={x:Type xct:Expander}}}"/>
     </Grid.GestureRecognizers>

    </Grid>

PS: Xaml is not mandatory. If possible, c# is also OK.

Edit: And I get this warning

Binding: 'MyProject.Forms.ViewElements.Controls.ExtendedExpander' cannot be converted to type 'System.Windows.Input.ICommand'

CodePudding user response:

Add Path=<Name of command property> in the GestureRecognizer; see CollectionView example below:

<CollectionView 
  ...
  RemainingItemsThresholdReachedCommand="{Binding Source={RelativeSource                                                        
                                                    AncestorType={x:Type vm:MyViewModel}},
                                                  Path=ThresholdReachedCommand}"
  ...
<Grid>
  <Grid.GestureRecognizers>
    <TapGestureRecognizer Command="{Binding Source={RelativeSource 
                                              AncestorType={x:Type CollectionView}},
                                            Path=RemainingItemsThresholdReachedCommand}" 
  ...
  • Related