私は 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;
}
しかし、実際には機能していません。つまり、機能しますが、毎回ではありません。私が最大の場合。ウィンドウのサイズが変更されないことがあるため、コントロールのサイズが変更された場合は少し「ランダム」です。したがって、誰かがより良い解決策を持っているとしたら、それはとてもいいことです。