gfache
gfache

Reputation: 616

How to create a forwarding bindable property in a composite control

I wonder if it is actually possible to create a composite control that has common attributes with child controls and declare bindable forwarding properties without having all the boilerplate?

By example I have this bindable property, MaxShownItems declared like a standard binding. The thing is there is a lot of plumbing here, just for this last line of code inside the property changed callback...

#region MaxShownItems (Bindable int)
/// <summary>
/// Manages the binding of the <see cref="MaxShownItems"/> property
/// </summary>
public static readonly BindableProperty MaxShownItemsProperty
    = BindableProperty.Create(propertyName: nameof(MaxShownItems)
                            , returnType: typeof(int)
                            , declaringType: typeof(BoardView)
                            , defaultValue: default(int)
                            , defaultBindingMode: BindingMode.OneWay
                            , propertyChanged: MaxShownItems_PropertyChanged
                            );

public int MaxShownItems
{
    get { return (int)GetValue(MaxShownItemsProperty); }
    set { SetValue(MaxShownItemsProperty, value); }
}

private static void MaxShownItems_PropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
    var myBoardView = (BoardView)bindable;
    var max = Math.Max(newValue as int? ?? 0, 0);
    myBoardView.BoardLayout.MaxShownItems = max;
}
#endregion  //  MaxShownItems (Bindable int)

My understanding of the binding mechanism is quite new but I don't think that is actually possible. Doesn't hurt to ask, right?

Upvotes: 1

Views: 207

Answers (0)

Related Questions