UWP XAML中的自定义边框控件

时间:2018-07-01 23:40:40

标签: c# xaml uwp

您可以通过继承Panel类并覆盖MeasureOverrideArrangeOverride在UWP XAML中创建自定义面板。对于只有一个孩子的小组,我该如何做类似Border控件的事情?不幸的是,我无法从Border派生出来,因为它是密封的。

我尝试了此操作,但没有用(当然,这是我要实现的目标的简化示例):

public sealed class ProportionedPresenter : FrameworkElement
{
    public static readonly DependencyProperty ChildProperty = DependencyProperty.Register(
        "Child",
        typeof(object),
        typeof(ProportionedPresenter),
        new PropertyMetadata(null));

    public object Child
    {
        get { return this.GetValue(ProportionedPresenter.ChildProperty); }
        set { this.SetValue(ProportionedPresenter.ChildProperty, value); }
    }

    protected override Size MeasureOverride(Size availableSize)
    {
        var minDimension = Math.Min(availableSize.Width, availableSize.Height);
        var desiredSize = new Size(minDimension, minDimension);
        if (this.Child is UIElement childElement)
        {
            childElement.Measure(desiredSize);
            var maxDimension = Math.Max(childElement.DesiredSize.Width, childElement.DesiredSize.Height);
            desiredSize = new Size(maxDimension, maxDimension);
        }
        return desiredSize;
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        if (this.Child is UIElement childElement)
        {
            childElement.Arrange(new Rect(new Point(), childElement.DesiredSize));
        }
        return finalSize;
    }
}

0 个答案:

没有答案
相关问题