1

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?

gfache
  • 606
  • 6
  • 15
  • you can directly bind the `BoardLayout.MaxShownItems` to parent control's (`BoardView.MaxShownItems`) property while using property coercion. –  Nov 13 '17 at 16:47
  • I just had a quick look at the property coercion and I see that this example is buggy but implementing the coercion delegate would need quite the same amount of code than this, if I understood correctly? – gfache Nov 21 '17 at 15:10

0 Answers0