RadSlideView Access Parent BindingContext in Slide

Environment

Product Version 2018.3.1122.3
Product SlideView for Xamarin.Forms

Description

The RadSlideView uses ContentView for the slides, these ContentViews do not have scope to BindingContext of the visual parents (e.g. the SlideView or parent page). This has the consequence of not being able to bind any ContentView content to a page view model.

Solution

You can solve this by using a static helper class to share a BindingContext reference. The static class only needs one public static object property:

Example Helper Class

public static class BindingContextHelper
{
    public static object CurrentPageBindingContext { get; set; }
}

Example Use

When the RadSlideView parent's BindingContext is set, you can set the helper class's property. In this example, we'll use a ContentPage.

Model

public class MyItem
{
    public string ItemName { get; set; }
}

ViewModel

public class ViewModel
{
    public ViewModel()
    {
    }

    public ObservableCollection<MyItem> MyItems { get; set; } = new ObservableCollection<MyItem>
    {
        new MyItem {ItemName = "Item One"}
    };

    public Command MyViewModelCommand { get; set; } = new Command(() =>
    {
            Debug.WriteLine("You have invoked a command in the view model!");
    });
}

View

<telerikPrimitives:RadSlideView ItemsSource="{Binding MyItems}">
  <telerikPrimitives:RadSlideView.ItemTemplate>
    <DataTemplate>
      <ContentView>
        <StackLayout >
          <Label Text="{Binding ItemName}" />
          <Button BindingContext="{x:Static helpers:BindingContextHelper.CurrentPageBindingContext}"
                  Command="{Binding MyViewModelCommand}" />
                </Grid>
            </ContentView>
        </DataTemplate>
    </telerikPrimitives:RadSlideView.ItemTemplate>
</telerikPrimitives:RadSlideView>
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        // Set the parent's BindingContext
        this.BindingContext = new ViewModel();

        // Pass that BindingContext to the helper class
        BindingContextHelper.CurrentPageBindingContext = this.BindingContext;
    }
}
In this article
Not finding the help you need? Improve this article