Bind to button BackgroundColor


Is it possible to bind to a buttons background color?

I have tried this. But wit no success.

var b = new Button { HeightRequest = 5, Text = item.Title, Font = Font.SystemFontOfSize(20, FontAttributes.Bold), HorizontalOptions = LayoutOptions.Start, }; b.SetBinding(Button.BackgroundColorProperty, "StatusColor");

is it possible to bind the background color. And the text color of the button?



  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    You have to set b.BindingContext with something that has a StatusColor property

  • CaptainXamtasticCaptainXamtastic GBUniversity ✭✭✭
    edited August 2015

    Morten, here's some cut and paste from one of my proof of concepts, although it uses different views, the concepts are the same, so long as you have the same properties exposed in the vieew you want, it will be the same code, in effect:

        var view = new Grid { };
        view.SetBinding(View.BackgroundColorProperty, "IsSelected", BindingMode.Default, new IsSelectedToBackgroundColorConverter(), null);
        var label = new Label { HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center, FontSize = Device.GetNamedSize(NamedSize.Micro, typeof(Label)) };
        label.SetBinding(Label.TextProperty, "Name", BindingMode.Default, null, null);
        label.SetBinding(Label.TextColorProperty, "IsSelected", BindingMode.Default, new IsSelectedToTextColorConverter(), null);
        view.BindingContext = viewmodel;
        return view;

    The IValueConverters are obvious, I hope!

    Naturally, what you are binding to as the BindingContext must implement INotifyPropertyChanged if you want the bindings to change when the associated property in the 'viewmodel' (the BindingContext) changes.

    In the above example, the properties that would need to be exposed in the ViewModel are IsSelected and Name.

    Here, the Grid's BackgroundColor, and the Color of the Text, toggle when IsSelected changes from True to False and back.

Sign In or Register to comment.