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

发布于 2024-08-11 19:28:15 字数 84 浏览 4 评论 0原文

有谁知道如何保持 UserControl 的高/宽比 1:1 吗?

例如,如果高度>宽度,宽度和宽度高度将具有相同的尺寸,反之亦然。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浮生面具三千个 2024-08-18 19:28:15

我不确定这是否有效,但如果您为 SizeChanged 事件,并在其中放入代码,保持宽高比 1:1。

SizeChangedEventArgs 参数有旧尺寸和新尺寸,以便您可以检查哪个尺寸已更改并相应更新另一个尺寸。

您可能需要引入一个保护变量,这样您就不会因更新 HeightWidth 而导致级联 SizeChanged 事件。

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.

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

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.

猫七 2024-08-18 19:28:15

另一种选择:

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

Another alternative:

<local:MyControl Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/>
蝶舞 2024-08-18 19:28:15

尝试使用 ViewBox 并将其 Stretch 属性设置为 Uniform

Try using a ViewBox and setting its Stretch property to Uniform

一直在等你来 2024-08-18 19:28:15

我使用此代码在用户控件内保持纵横比

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

祝你好运

i used this code for keeping aspect ratio

inside usercontrol globally define 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:

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

醉生梦死 2024-08-18 19:28:15
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;
        }
    }

也许这有帮助...干杯

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文