Bug 58605 - BoxView - Binding Color or BackgroundColor from C# doesn't work
Summary: BoxView - Binding Color or BackgroundColor from C# doesn't work
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.4
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-08-07 05:41 UTC by Nicholas Bauer
Modified: 2017-08-09 18:44 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Nicholas Bauer 2017-08-07 05:41:43 UTC
Attempting to use SetBinding between a BoxView's ColorProperty from a source color property in C# doesn't work, BoxView never renders. Setting the binding in XAML, and setting the property directly in both XAML and C#, all work.

Below is a simple example.

MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:XamarinTest"
             x:Class="XamarinTest.MainPage"
             x:Name="root">

    <Grid x:Name="grid" Padding="5" BackgroundColor="Black">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <BoxView Grid.Row="2" Color="{Binding BackgroundColor, Source={x:Reference root}}"/>

        <BoxView Grid.Row="3" Color="Red"/>
    </Grid>

</ContentPage>


MainPage.xaml.cs:

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace XamarinTest
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            BackgroundColor = Color.Red;

            var box1 = new BoxView();
            box1.SetBinding(BoxView.ColorProperty, new Binding(nameof(BackgroundColorProperty), source: this));
            Grid.SetRow(box1, 0);

            var box2 = new BoxView();
            box2.Color = Color.Red;
            Grid.SetRow(box2, 1);

            grid.Children.Add(box1);
            grid.Children.Add(box2);
        }
    }
}
Comment 1 Nicholas Bauer 2017-08-07 05:44:05 UTC
Top four rows should be Red; Last row is empty as a negative control.

No change if BackgroundColor is the target instead of Color.
Comment 2 Nicholas Bauer 2017-08-07 05:54:35 UTC
Fails on both Android and UWP.

Also confirmed on 2.3.5.256-pre6.
Comment 3 Jimmy [MSFT] 2017-08-09 18:27:32 UTC
The problem is that nameof(BackgroundColorProperty) returns "BackgroundColorProperty" so you are not binding to the actual property. Use the following instead:

> SetBinding(BoxView.ColorProperty, new Binding(BackgroundColorProperty.PropertyName, source: this));

or 

> SetBinding(BoxView.ColorProperty, new Binding(nameof(BackgroundColor), source: this));

Hope this helps!
Comment 4 Nicholas Bauer 2017-08-09 18:44:58 UTC
Oh... duh. That's mildly embarrassing. Thank you.