UserControl の高さ/幅の比率を 1:1 に保つ方法を知っている人はいますか?
たとえば、高さ > 幅の場合、幅と高さは同じサイズになり、その逆も同様です。
UserControl の高さ/幅の比率を 1:1 に保つ方法を知っている人はいますか?
たとえば、高さ > 幅の場合、幅と高さは同じサイズになり、その逆も同様です。
これが機能するかどうかはわかりませんが、SizeChanged
イベントのハンドラーを登録し、そこにコードを挿入すると、アスペクト比は 1:1 に保たれます。
引数にはSizeChangedEventArgs
古いサイズと新しいサイズがあるため、どちらが変更されたかを確認し、それに応じて他方を更新できます。
またはSizeChanged
の更新の結果としてイベントのカスケードを取得しないように、ガード変数を導入する必要がある場合があります。Height
Width
ViewBox を使用して、その Stretch プロパティを Uniform に設定してみてください
アスペクト比を維持するためにこのコードを使用しました
ユーザーコントロール内で、org_width、org_height、org_ratio をグローバルに定義します。
private static double org_width = 77.6;//desired width
private static double org_height = 81.4;//desired height
private static double org_ratio = org_width / org_height;
SizeChanged イベントのユーザー コントロール内で次のコードを使用します。
FrameworkElement UCborder = this;
UCborder.Width = UCborder.Height*org_ratio;
最後に、ユーザー コントロール コードは次のようになります。
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace yournamespace
{
public partial class YourUserControl : UserControl
{
private static double org_width = 77.6;//desired width
private static double org_height = 81.4;//desired height
private static double org_ratio = org_width / org_height; // width/height
public YourUserControl()
{
InitializeComponent();
}
private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
FrameworkElement UCborder = this;
UCborder.Width = UCborder.Height*org_ratio;
}
}
}
幸運を
private bool isSizeChangeDefered;
private void uiElement_SizeChanged(object sender, SizeChangedEventArgs e)
{
//Keep Acpect Ratio
const double factor = 1.8;
if(isSizeChangeDefered)
return;
isSizeChangeDefered = true;
try
{
if (e.WidthChanged)
{
driverPan.Height = e.NewSize.Width * factor;
}
if (e.HeightChanged)
{
driverPan.Height = e.NewSize.Width / factor;
}
}
finally
{
// e.Handled = true;
isSizeChangeDefered = false;
}
}
たぶんこれが役に立ちます...乾杯