Xamarin.Forms bindingContext Set the source back to root/parent

Viewed 5195

I got a ViewModel with a command (AddToFavoriteCommand) that doesn't get called. Now it only looks after the command in the CustomPin class, not the viewModel. I'm setting my viewModel to the BindingContext of the page in the code behind.

But since it enumerates my collection of customPins, it looks after the command there. I need to get back to the root. I probably need to change the source but can't get it to work.

<ContentPage.Content>
    <StackLayout>
        <AbsoluteLayout>
            <Button Text="{Binding Filter}" Command="{Binding GotoFilterPageCommand}" />
        </AbsoluteLayout>
        <ListView x:Name="ListView" RowHeight="60" ItemsSource="{Binding CustomPins}" ItemSelected="OnItemSelected">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.ContextActions>
                            <MenuItem Text="Favorite" Command="{Binding AddToFavoriteCommand}" />
                            <MenuItem Text="..." CommandParameter="{Binding .}" Clicked="OnMoreClicked" />
                        </ViewCell.ContextActions>
                        <StackLayout Padding="5" Orientation="Vertical" >
                            <Label Text="{Binding ParkingLot.Street}" FontSize="Medium" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>

Code behind (removed all other logic like clicked events that I didn't needed for this)

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ParkingListPage : ContentPage
{
    public ParkingListPage()
    {
        InitializeComponent();
        BindingContext = new ParkingListViewModel();
    }
}

My ViewModel

public class ParkingListViewModel
{
    public ParkingListViewModel()
    {
        AddToFavoriteCommand = new Command(Test);
    }

    private void Test()
    {
    }

    public IEnumerable<CustomPin> CustomPins { get; set; } = SampleParkings.Parkings;

    public Command AddToFavoriteCommand { get; }
}
1 Answers
Related