![](/img/trans.png)
[英]WPF/C# Textwrapping in a scrollviewer makes window resizing laggy
[英]Resizing wpf window programmatically in c#
我有一个带有两个用户控件的 wpf 窗口,其中第二个仅在需要时显示。 我只在 XAML 中设置了窗口的 MinWidth,MinHeight 是通过数据绑定提供的,根据是否显示第二个用户控件设置了一个列表。 现在:如何在运行时将窗口的大小设置为与 MinWidth/Height 不同的值。 我尝试在 Show() 之前、Show() 之后、在各种事件(初始化、加载等)中设置值。 我尝试使用和不使用 UpdateLayout(),我尝试通过数据绑定设置高度/宽度。 没有任何作用! 但是当我调试方法时,我看到窗口的高度/宽度属性设置为预期值,但实际高度/宽度保持不变。 我以为这会是一个小事,但事实证明它不是(对我来说)。 你的任何帮助。
你有没有试过设置
Application.Current.MainWindow.Height = 100;
简短补充:我刚刚在代码中做了一个简短的测试:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private int _height;
public int CustomHeight
{
get { return _height; }
set
{
if (value != _height)
{
_height = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("CustomHeight"));
}
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
CustomHeight = 500;
}
public event PropertyChangedEventHandler PropertyChanged;
private void Button_Click(object sender, RoutedEventArgs e)
{
CustomHeight = 100;
}
}
和 XAML:
<Window x:Class="WindowSizeTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="{Binding CustomHeight, Mode=TwoWay}" Width="525">
<Grid>
<Button Click="Button_Click">Test</Button>
</Grid>
</Window>
单击按钮设置窗口高度。 这就是你要找的吗?
如果有人仍在为此苦苦挣扎,那么您唯一要做的就是:
如果要调整主窗口的大小,只需编写以下代码。
Application.Current.MainWindow.Height = 420;
如果要调整主窗口以外的新窗口的大小,只需在新窗口的 .cs 文件中编写以下代码即可。
Application.Current.MainWindow = this;
Application.Current.MainWindow.Width = 420;
希望能帮助到你。
你没有提供太多信息,所以我只是在这里猜测。
我可以在不考虑其宽度和高度设置的情况下重现窗口的唯一方法是将SizeToContent
设置为WidthAndHeight
。
我只是简单地设置了窗口高度属性。
原本_myWindow.Height = 400;
一旦内容需要调整大小,我将其设置如下:
double newWindowHeight = a + b;
其中 a + b 使窗口更大,因此:
_myWindow.Height = newWindowHeight;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.