您可以通过继承Panel
类并覆盖MeasureOverride
和ArrangeOverride
在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;
}
}