Access a Control Method from a ViewModel

Making a control in your view MVVM friendly by allowing binding to your ViewModel, can be done in several different ways. When developing in the MVVM pattern, you have no access to the View and hence no access to the methods on a Control. Most controls in Xamarin Forms have bindable properties but some do not. In order to keep to our MVVM Pattern there are numerous ways we can access the control’s methods. I have these ordered from my least preferable to my most preferable.


For a common example I will be using the GoBack method of the WebView, which is only accessible directly via the control.

Boolean Flip

A common recommendation you might see around is extending the control and adding a boolean to it. First we must extend the control and add a boolean property of IsGoBack.

public class CustomWebView : WebView
    public bool IsGoBack
        get { return (bool)GetValue(IsGoBackProperty); }
        set { SetValue(IsGoBackProperty, value); }

    public static BindableProperty IsGoBackProperty = BindableProperty.Create(nameof(IsGoBack), typeof(bool), typeof(EnhancedWebView), propertyChanged: (bindable, oldValue, newValue) =>
        var webView = binadable as CustomWebView;

Then in your View you just need to bind this property to a propery in your ViewModel.

<control:CustomWebView IsGoBack="{Binding IsGoBack}" />

Now doing this in your ViewModel will cause the PropertyChanged event to be fired and you can call the GoBack method.

private bool _isGoBack;
public string IsGoBack
         return _isGoBack;
         _isGoBack = value;

// Then call anywhere in your ViewModel by doing
IsGoBack = !IsGoBack;


  • Keeps your View and ViewModel separated


  • Extra steps and complexity in relaying a command


Messaging Center is a Xamarin Forms implementation of a publish and subscribe model. First we subscribe to an event to listen to in the View.

MessagingCenter.Subscribe<object>(this, "GoBack", (sender) => {

Then when we need to call this, we publish a message.

MessagingCenter.Send<object>(this, "GoBack");


  • Easy to implement


  • You need to subscribe and listen to events in your View.
  • You don’t know what is listening or can not get any direct response back.

Supervising Controller Pattern

This Supervising Controller Pattern in this context is when the ViewModel will be aware of an interface to the View and the View will inject itself back into the ViewModel.

First we need to apply an interface to the Page (View).

public interface IView 
    void GoBack();

Then we implement the interface on the View.

public class MainPage: ContentPage, IView

Then we send the View back to the ViewModel.

public MainPage()
    this.BindingContext = MainViewModelInstance;
    MainViewModelInstance.View = this;

Finally in the ViewModel we can call the View as needed to interact with the control.

public IView View { get; set; }



  • No modification or extension of the control


  •  Must pass the view back into the ViewModel, which ties the ViewModel type to the View.

Command Chaining

Command chaining is the most MVVM friendly approach as it leaves everything to the binding system to connect up and the View and ViewModel still have no direct knowledge of each other. The only issue is you must extend the control and it adds a bit of extra complexity.

First we need to extend the control to add a new Bindable Property with a OneWayToSource. This ensures this property sends its value back to the ViewModel and not the other way around.

public static BindableProperty GoBackCommandProperty =
 BindableProperty.Create(nameof(GoBackCommand), typeof(ICommand), typeof(CustomWebView), null, BindingMode.OneWayToSource);

public ICommand GoBackCommand
    get { return (ICommand)GetValue(GoBackCommandProperty); }
    set { SetValue(GoBackCommandProperty, value); }

Then we need to assign the GoBack function to the Bindable Property.

public CustomWebView()
    GoBackCommand = new Command(() =>

Then we add a GoBackCommand to the ViewModel.

public ICommand GoBackCommand
    get { return GetValue<ICommand>(); }
    set { SetValue(value); }

Now we can Bind the Command directly to another Command that we can execute.

<control:CustomWebView GoBackCommand="{Binding GoBackCommand, Mode=OneWayToSource}" />

<Button Text="Go Back!" Command="{Binding GoBackCommand}" />


  • View and ViewModel have no reference to each other, everything is connected via the Binding system.


  • Can be a bit complex to implement and debug


  1. M. Prades


    Thanks you for your publication, that’s useful!

    I try to implement your “Command Chaining” part in my project, however I don’t understand how you use “GetValue();” and “SetValue(value);” in your ViewModel (to add GoBackCommand).

    Are they generic methods? I don’t found them and it’s the last piece of the puzzle to implement your solution.

    Thanks for your help,

    Best regards.

    1. Adam Pedley

      The GetValue and SetValue are methods in Xamarin Forms BindableObject class, which WebView inherits from. If you inherit from any Xamarin Forms control, you should have access to these methods.

      These methods only store the data, and trigger INotifyPropertyChanged when the value is changed.

      1. Mel

        Hi Adam,

        Sorry I still don’t quite understand how these methods can be used. If they are on the viewmodel, how can we access them via a control which won’t be referenced on the viewmodel?

  2. Dave Smalley

    I have used the “command chaining” technique with one additional component.

    Rather than adding the bindable command property to the control, I define it in a behavior and initialize the command in the behavior’s constructor. This avoids sub-classing the control, and works as long as the command’s implementation relies on public methods and/or properties. Obviously, if it requires protected access only sub-classing is possible.

    I derive my behavior from the Prism library’s BehaviorBase class, which has a strongly typed AssociatedObject property to access the control to which the behavior is attached.

    I just don’t like the idea of using MyButton, MyEntry, MyListView etc. controls to simply expose a control method to the view model. This seems cleaner.