Xamarin Android Binding to custom control

May 11, 2015 at 11:31 AM
Hi Laurent,

Is there a specific requirement a property has to have in order for binding to work?
I ask this because I've been using the bindings for a while and they work with EditText and TextView and ListView controls but I'm now trying to use a custom control and the binding is failing.
() => ViewModel.Theme.Coloring.Foreground,
() => ThemeForegroundColorPanel.Color,
The ColorPanel is a simple rectangle and has a public property named Color with a setter and getter and that raises a propertychanged event when the value is set.

The interesting part is that when the page loads, the ColorPanel gets set with the Foreground color from the ViewModel corrently, but uppon changing the Color property on the ColorPanel the source property never gets updated and stepping through the propertychanged event handler:
PropertyChangedEventHandler eventHandler = this.PropertyChanged;
if (eventHandler != null)
    eventHandler(this, new PropertyChangedEventArgs(propertyName));
eventHandler is null.

Any help with this situation would be welcome since I have like 30 of these ColorPanels on my application.
May 11, 2015 at 1:11 PM
Edited May 11, 2015 at 1:11 PM

You need to use the UpdateTargetTrigger method to set the name of the Event that the panel is raising (in this case PropertyChanged).

In Windows XAML, all UI properties are DependencyProperties, so they are observable. For Android however, there is no such mechanism. I am helping by assuming that the default event is TextChanged for EditText elements, but I don't make other assumptions. As far as I can tell, no default controls use PropertyChanged to notify that one of their properties changed, so I didn't observe this event in particular.

Check the inline doc for information about how UpdateTargetTrigger works.

Hopefully that helps
May 11, 2015 at 3:13 PM
Hi Laurent,

Thanks for the quick reply. After adding .UpdateTargetTrigger("PropertyChanged") I'm now getting an Invalid Cast somewhere inside the binding expression although both my converters behave as expected and are not being called when the error happens. I'll have to digg deeper into your code.

Also, while looking through the comments in the source code I found the following in the Android Platform folder (BindingGeneric.cs):
public Binding<TSource, TTarget> UpdateTargetTrigger()
    return UpdateSourceTrigger(UpdateTriggerMode.PropertyChanged);
Shouldn't it return UpdateTargetTrigger(...) ? (Hope I'm not making a fool of myself).

Thanks again,

Diogo Mendonça
May 11, 2015 at 6:37 PM
Edited May 11, 2015 at 6:38 PM
Well, removed the ColorPanel and created my own (just extended a View and added an overwriting property called BackgroundColor and raised its PropertyChanged event) and it worked flawlessly. There must be a problem with the event bubbling because I could see the PropertyChanged event being raised in the previous custom control but the Binding was never updating.

Now it works, although I would like to know why the previous control didn't. Since this was such a simple control I was able to create one of my own but if it was more complex this would be a problem.

Thank you very much Laurent.
May 11, 2015 at 9:57 PM
Happy to take a look at the one that fails if you send me a repro.

May 14, 2015 at 11:09 AM
Ok, I'll create a simple project this weekend and paste a link for you here.
May 14, 2015 at 11:23 AM
That's very helpful, thanks!!