Forum Xamarin.Forms

How to create a TwoWay Binding in a ContentView

I try to create a custom content view with a two way binding but the binding does just work from the view model to the view.

The view snippet:

public class MyContentView : ContentView
public static readonly BindableProperty MyProperty = BindableProperty.Create("My", typeof(bool), typeof(MyContentView), false, BindingMode.TwoWay);

public bool My
        get { return (bool) GetValue(MyProperty); }
        set { SetValue(MyProperty, value); }


The view model code:

public class MyContentViewModel : ViewModelBase
public bool IsActive
get { return _isActive; }
_isActive= value;

The "putting together":

var view = new MyContentView();
var viewmodel = new MyContentViewModel();
view.BindingContext = viewmodel;
var b = new Binding("IsActive", BindingMode.TwoWay);
view .SetBinding(MyContentView.MyProperty, b);

When setting the view.My property the value is update through the binding in the view model, but when setting the IsActive property in the viewmodel the My property in the view is not updated at all.

Any hints?

Sign In or Register to comment.