简体   繁体   English

WPF禁用窗口自动调整大小

[英]WPF disable window auto resize

Currently I have 目前我有

    SizeToContent="WidthAndHeight" 
    ResizeMode="NoResize"

because I want to take advantage of WPF's auto resize. 因为我想利用WPF的自动调整大小。 But when I hide some buttons the window width shrinks, this is expected behavior but I would like to disable that. 但是,当我隐藏一些按钮时,窗口宽度会缩小,这是预期的行为,但我想禁用它。 Basically I want the window size to stay as it is after it is rendered. 基本上,我希望窗口大小在渲染后保持原样。 Is there a nice way to do that instead of calculating the width on start up and set it in code behind? 有什么好方法可以代替在启动时计算宽度并在后面的代码中进行设置吗?

There is nothing wrong with code behind. 后面的代码没有错。

But you might want to set your buttons' Visibility to Hidden instead of Collapse to achive the desired behaviour. 但是您可能希望将按钮的“ Visibility ”设置为“ Hidden而不是“ Collapse以实现所需的行为。

You can set the size (fix size) of your window in its load event when the window got its width and height based on the size of its content. 当窗口根据其内容的大小获得其宽度和高度时,可以在其load事件中设置窗口的大小(固定大小)。 You should can use something like this.Width = this.ActualWidth and this.Height= this.ActualHeight, in the load event. 您应该在加载事件中使用类似this.Width = this.ActualWidth和this.Height = this.ActualHeight的方法。

Here is what i do : For Window Set: 这是我的工作:对于窗口集:

 SizeToContent = SizeToContent.WidthAndHeight
 ContentRendered += DisableSizeToContent

Finally disable SizeToContent there: 最后在此处禁用SizeToContent

private static void DisableSizeToContent(object sender, EventArgs e)
{
    if (sender is Window win)
    {                
        win.SizeToContent = SizeToContent.Manual;
    }
}

Still looking a solution for Async Bindings! 仍在寻找异步绑定的解决方案!

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

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