1

我想知道是否真的可以创建一个复合控件,它具有与子控件的共同属性并声明可绑定的转发属性而无需所有样板文件?

例如,我有这个可绑定的属性,MaxShownItems声明为标准绑定。问题是这里有很多管道,只是为了属性更改回调中的最后一行代码......

#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)

我对绑定机制的理解是相当新的,但我认为这实际上是不可能的。问一下也无妨,对吧?

4

0 回答 0