Skip to main content

Xamarin.Forms BindableProperty's unintuitive getter/setter properties

Apparently you cannot debug (or break into) these getter/setter properties of a BindableProperty and instead are advised to implement a propertyChanged event on the BindableProperty.
I made a userControl (as a refactored view w/in a view) and wanted to pass two bindable contexts to separate views w/in the parent view; something like this:


1
2
<lv:MasterDetailArrableView Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
    MasterArrable="{Binding FieldSelectionKeys}" DetailArrable="{Binding FieldSelectionFields}"/>

I decided to implement this by setting the internal view's BindingContext when the respective properties are set. However you cannot simply overwrite the MasterArrable setter and instead are advised to catch the propertyChanged event and do your work there. For example, the MasterArrable BindableProperty code looks like this:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public static readonly BindableProperty MasterArrableProperty = BindableProperty.Create(
    propertyName: "MasterArrable",
    returnType: typeof(ObservableCollection<KeyValueArrable>),
    declaringType: typeof(MasterDetailArrableView),
    defaultValue: null, 
    defaultBindingMode: BindingMode.TwoWay, 
    propertyChanged: OnMasterArrableChanged);

public ObservableCollection<KeyValueArrable> MasterArrable
{
    get { return (ObservableCollection<KeyValueArrable>)GetValue(MasterArrableProperty); }
    set { SetValue(MasterArrableProperty, value); }
}

private static void OnMasterArrableChanged(BindableObject bindable, object oldValue, object newValue)
{
    MasterDetailArrableView view = bindable as MasterDetailArrableView;
    ObservableCollection<KeyValueArrable> arrables = (ObservableCollection<KeyValueArrable>)newValue;
    view.MasterArrableView.BindingContext = arrables;
}  

Thanks for WiktorKoncki and Thib's posts at: https://forums.xamarin.com/discussion/66026/use-attached-bindable-property-in-xaml

Comments

Popular posts from this blog

Xamarin.Forms OnPlatform - TabbedPage vs NavigationPage

I switched my start page from a NavigationPage to a TabbedPage and all of a sudden the content overwrote the the carrier/battery row. I looked into this and discovered that the TabbedPage does not offset itself below this header in iOS (but works for android and windows). The solution is to add a platform dependent offset or padding thickness of 20 pixels.

dbapps.com: top 200 iOS apps

Repurposed dbapps.com to present the iOS app data I've collected over the years. It shows the top 200 FREE, PAID, and GROSSING iOS apps on the apple app store along w/ their deltas over several time periods. I've used this report to help guide my investments in the mobile app space. Here's a screenshot: