WPF ResizeMode 改变窗口大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19838385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF ResizeMode Changes Window Size
提问by mastur cheef
I am currently making a wpf application and want to disable window resizing during certain operations. I have been using this.ResizeMode = System.Windows.ResizeMode.CanMinimize;, which does exactly what I want, but it makes the window larger than before.
我目前正在制作一个 wpf 应用程序,并希望在某些操作期间禁用窗口大小调整。我一直在使用this.ResizeMode = System.Windows.ResizeMode.CanMinimize;,这正是我想要的,但它使窗口比以前更大。
I don't know if it's possible, but is there a way to avoid the enlarging of the window?
我不知道是否可能,但是有没有办法避免窗口扩大?
EDIT:
编辑:
Here's a very simple wpf application that demonstrates this problem.
这是一个非常简单的 wpf 应用程序,它演示了这个问题。
xaml:
xml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Trigger CanMinimize" Height="23" HorizontalAlignment="Left" Margin="62,248,0,0" Name="canMin" VerticalAlignment="Top" Width="120" Click="canMin_Click" />
<Button Content="Trigger CanResize" Height="23" HorizontalAlignment="Left" Margin="327,248,0,0" Name="canRes" VerticalAlignment="Top" Width="108" Click="canRes_Click" />
</Grid>
</Window>
cs file:
cs文件:
using System.Windows;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void canMin_Click(object sender, RoutedEventArgs e)
{
this.ResizeMode = System.Windows.ResizeMode.CanMinimize;
}
private void canRes_Click(object sender, RoutedEventArgs e)
{
this.ResizeMode = System.Windows.ResizeMode.CanResize;
}
}
}
回答by Andras Sebo
Actualy I don't really understand what "makes window larger" means, but alternatively you can set
实际上我真的不明白“使窗口更大”是什么意思,但你也可以设置
MaxWidth = ActualWidth;
MinWidth = ActualWidth;
MaxHeight = ActualHeight;
MinHeight = ActualHeight;
instead of
代替
this.ResizeMode = System.Windows.ResizeMode.CanResize;
In case you want to re-enable resize set those properties to double.NaN.
如果您想重新启用调整大小,请将这些属性设置为double.NaN.
.
.
回答by J W
To disallow resizing simply do the following:
要禁止调整大小,只需执行以下操作:
ResizeMode = ResizeMode.NoResize;
ResizeMode = ResizeMode.NoResize;
It will also remove the minimize and maximize buttons.
它还将删除最小化和最大化按钮。

