简体   繁体   English

如何强制调整窗口大小?

[英]How do I force my window to re-size?

I have an auto-sized application user control in WPF (Height="Auto" Width="Auto"). 我在WPF中有一个自动调整大小的应用程序用户控件 (Height =“ Auto” Width =“ Auto”)。 When a bunch of elements inside it are resized, the windows stays the same rendered size. 当调整其中一堆元素的大小时,窗口将保持相同的渲染大小。 How do I force it to resize? 如何强制调整大小? I have tried this line Application.Current.MainWindow.SizeToContent = SizeToContent.WidthAndHeight; 我已经尝试过这一行Application.Current.MainWindow.SizeToContent = SizeToContent.WidthAndHeight; in the function that resizes the components inside the window. 在该函数中调整窗口内部组件的大小。 The compiler gives me an error that there is no object associated with this call. 编译器给我一个错误,没有与此调用关联的对象。 I have tried this line MainWindowBorder.Height = Double.NaN; 我已经尝试过这一行MainWindowBorder.Height = Double.NaN; to trick it to resize. 欺骗它来调整大小。 No luck either. 也没有运气。

If all the inside controls can be resized but the only one thing to resize is Window, How about this simple approach? 如果所有内部控件都可以调整大小,但是唯一要调整的是Window,那么这种简单的方法呢? Hope this helps.. 希望这可以帮助..

int mycurrentscreenwidth = 1366;
int mycurrentwindowwidth = 1366;

var screen = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
var screenwidth = screen.Width;

if (Convert.ToInt32(screenwidth) < 1366)
{   
    double calculate_scalefactor= Convert.ToInt32(screenwidth) / (double)mycurrentscreenwidth;
    double newwidth_tobescaled = mycurrentwindowwidth * calculate_scalefactor;

    this.Width = newwidth_tobescaled;
}

To obtain the window of your current user control, see: 要获取当前用户控件的窗口,请参见:

Access parent window from User Control 从用户控件访问父窗口

Then use: 然后使用:

Window yourParentWindow = Window.GetWindow(userControl);
yourParentWindow.SizeToContent = SizeToContent.WidthAndHeight;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM