我有一个 UserControl,并且该 UserControl 必须根据宽高比调整大小。
这意味着:宽度:高度= 2:1。
目前我正在使用这段代码:
protected override Size ArrangeOverride(Size arrangeBounds)
{
if (ActualWidth == 0 || ActualHeight == 0) return arrangeBounds;
base.ArrangeOverride(arrangeBounds);
double ratio = 2;
if (Parent != null)
{
var size = new Size(arrangeBounds.Height * ratio, arrangeBounds.Height);
double containerWidth = ((FrameworkElement)Parent).ActualWidth;
if (containerWidth < size.Width)
{
double newHeight = arrangeBounds.Height * (containerWidth / size.Width);
canvas.Width = newHeight * ratio;
canvas.Height = newHeight;
}
else
{
canvas.Width = size.Height * ratio;
canvas.Height = size.Height;
}
}
return arrangeBounds;
}
但这并没有真正发挥作用。这意味着它有效,但并非每次都有效。如果我最大。窗口有时不会调整大小,因此如果控件调整大小,则有点“随机”。因此,如果有人有更好的解决方案,那就太好了。
最直接的解决方案是通过值转换器将高度直接绑定到宽度。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)