This project has moved and is read-only. For the latest updates, please go here.

Xamarin Android Binding to custom control

May 11, 2015 at 12:31 PM
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 2:11 PM
Edited May 11, 2015 at 2: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 4: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 7:37 PM
Edited May 11, 2015 at 7: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 10:57 PM
Happy to take a look at the one that fails if you send me a repro.

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