Rikkert
Rikkert

Reputation: 100

How to bind CollectionView.EmptyView to the page's ViewModel?

I'm building an app with a CollectionView, where I'd like the user to add an item to an ObservableCollection when the CollectionView is empty. Problem is: I cannot find out how to bind to the page's ViewModel. This is a Xamarin.Forms/MvvmCross project

I tried setting the BindingContext, but I didn't succeed.

<CollectionView.EmptyView>
    <Grid Padding="24,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="154" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="154" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>
        <pan:PancakeView
            CornerRadius="8"
            WidthRequest="154"
            HeightRequest="154"
            IsClippedToBounds="True"
            HorizontalOptions="FillAndExpand"
            BackgroundColor="#FF252525"
            VerticalOptions="FillAndExpand"
            Elevation="2">
            <pan:PancakeView.HasShadow>
                <OnPlatform
                    x:TypeArguments="x:Boolean"
                    iOS="False"
                    Android="True" />
            </pan:PancakeView.HasShadow>
            <ImageButton
                HorizontalOptions="FillAndExpand"
                Padding="50"
                VerticalOptions="FillAndExpand"
                BackgroundColor="#FF252525"
                BindingContext="{Binding .}"
                Command="{Binding AddPlaylistCommand}"
                Source="icon_plus" />
        </pan:PancakeView>
    </Grid>
</CollectionView.EmptyView>

Command binding doesn't work, I'd like my command to just bind to the page's Viewmodel.

Upvotes: 0

Views: 398

Answers (1)

Rajesh Sonar
Rajesh Sonar

Reputation: 289

Try this:

 <ImageButton
            HorizontalOptions="FillAndExpand"
            Padding="50"
            VerticalOptions="FillAndExpand"
            BackgroundColor="#FF252525"
            Command="{Binding Source={x:Reference Name=myPage}, Path=BindingContext.DataContext.AddPlaylistCommand}"
            Source="icon_plus" />

Where myPage is is the Name of your ContentPage.

Upvotes: 2

Related Questions