如何保持用户控件的纵横比?

时间:2022-08-27 10:49:48

Does anyone have an idea how to keep the Height/Width Ratio 1:1 of a UserControl?

有没有人知道如何保持用户控件的高度/宽度比为1:1 ?

E.g. if Height > Width, Width & Height will have the same size and vice versa.

如果高度>宽度,宽度和高度将有相同的大小,反之亦然。

5 个解决方案

#1


7  

I'm not sure this will work, but if you register a handler for the SizeChanged event and in there put in your code keep the aspect ratio 1:1.

我不确定这是否可行,但如果您为SizeChanged事件注册一个处理程序,并在其中放入代码,那么请将纵横比保持为1:1。

The SizeChangedEventArgs argument has the old size and the new size so you can check which has changed and update the other accordingly.

SizeChangedEventArgs参数具有旧的大小和新的大小,因此您可以检查哪个已经更改,并相应地更新另一个。

You might need to introduce a guard variable so that you don't get a cascade of SizeChanged events as a result of updating the Height or Width.

您可能需要引入一个保护变量,以便不会因为更新高度或宽度而得到大小更改事件的级联。

#2


3  

Try using a ViewBox and setting its Stretch property to Uniform

尝试使用一个视图框并将其拉伸属性设置为制服

#3


3  

Another alternative:

另一个选择:

<local:MyControl Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/>

#4


0  

i used this code for keeping aspect ratio

我用这段代码来保持纵横比

inside usercontrol globally define org_width, org_height, org_ratio :

usercontrol内全局定义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;

use this code inside usercontrol in SizeChanged event:

在SizeChanged事件中,在usercontrol中使用此代码:

FrameworkElement UCborder = this;
UCborder.Width = UCborder.Height*org_ratio;

and finally your user control code should looks like this:

最后,您的用户控制代码应该是这样的:

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;
     }
  }
}

good luck

祝你好运

#5


-2  

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;
        }
    }

maybe this helps... cheers

也许这可以帮助…干杯

#1


7  

I'm not sure this will work, but if you register a handler for the SizeChanged event and in there put in your code keep the aspect ratio 1:1.

我不确定这是否可行,但如果您为SizeChanged事件注册一个处理程序,并在其中放入代码,那么请将纵横比保持为1:1。

The SizeChangedEventArgs argument has the old size and the new size so you can check which has changed and update the other accordingly.

SizeChangedEventArgs参数具有旧的大小和新的大小,因此您可以检查哪个已经更改,并相应地更新另一个。

You might need to introduce a guard variable so that you don't get a cascade of SizeChanged events as a result of updating the Height or Width.

您可能需要引入一个保护变量,以便不会因为更新高度或宽度而得到大小更改事件的级联。

#2


3  

Try using a ViewBox and setting its Stretch property to Uniform

尝试使用一个视图框并将其拉伸属性设置为制服

#3


3  

Another alternative:

另一个选择:

<local:MyControl Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/>

#4


0  

i used this code for keeping aspect ratio

我用这段代码来保持纵横比

inside usercontrol globally define org_width, org_height, org_ratio :

usercontrol内全局定义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;

use this code inside usercontrol in SizeChanged event:

在SizeChanged事件中,在usercontrol中使用此代码:

FrameworkElement UCborder = this;
UCborder.Width = UCborder.Height*org_ratio;

and finally your user control code should looks like this:

最后,您的用户控制代码应该是这样的:

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;
     }
  }
}

good luck

祝你好运

#5


-2  

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;
        }
    }

maybe this helps... cheers

也许这可以帮助…干杯