0

Some simplified code:

MainPage.xaml:

<ContentPage x:Name="MyMainPage">
    <ContentPage.Content>
        <Grid>
            <xForms:SfCarousel ItemsSource="{Binding SelectedBoard.ColumnsCollection}">
                <xForms:SfCarousel.ItemTemplate>
                    <DataTemplate>
                        <controls:CarouselItem/>
                    </DataTemplate>
                </xForms:SfCarousel.ItemTemplate>
            </xForms:SfCarousel>
        </Grid>
    </ContentPage.Content>
</ContentPage>

CarouselItem.xaml:

    <ContentView>
    <ContentView.Content>
        <Grid>
            <xForms:SfListView ItemsSource="{Binding TaskCollection}">
                <xForms:SfListView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout>
                            <Button x:Name="MyButton" CommandParameter="{Binding .}" Command=""/>
                        </StackLayout>
                    </DataTemplate>
                </xForms:SfListView.ItemTemplate>
        </Grid>
    </ContentView.Content>
</ContentView>

What I want to achieve: I want to, when I click "MyButton", fire Command defined in MainPageViewModel. I was trying to do that using

"Command="{Binding Path=BindingContext.MyCommand, Source={x:Reference Name=MyMainPage}}"

But then it throws "Can not find the object referenced by..." exception.

JoeDoe
  • 65
  • 7
  • You can't reference MyMainPage in CarouselItem.xaml:. One solution is write them in one file. And Have a look at this [thread](https://stackoverflow.com/questions/41753496/xamarin-binding-command-to-property-of-object-inside-user-control?rq=1) may help. – nevermore Apr 24 '19 at 07:44
  • Also [here](https://stackoverflow.com/questions/43881019/xamarin-custom-view-button-binding). – nevermore Apr 24 '19 at 08:10
  • If you want to control stuff across different views you should consider using services. Some class "AppService" can be injected (look for "Dependency Injection") into multiple view models to share data and actions. – japhwil May 02 '19 at 16:29

0 Answers0