在 C# 中以编程方式调整 wpf 窗口大小

发布于 2024-11-29 22:56:05 字数 321 浏览 1 评论 0原文

我有一个 wpf 窗口,其中有两个用户控件,其中第二个仅在需要时显示。我仅在 XAML 中设置窗口的 MinWidth,MinHeight 是通过数据绑定提供的,根据是否显示第二个用户控件设置。现在:如何在运行时将窗口的大小设置为与 MinWidth/Height 不同的值。我尝试在 Show() 之前、Show() 之后、在各种事件(初始化、加载等)中设置值。我尝试过使用和不使用 UpdateLayout(),我尝试通过数据绑定设置高度/宽度。什么都不起作用!但是,当我调试这些方法时,我发现窗口的高度/宽度属性设置为预期值,但实际高度/宽度保持不变。我以为这会是一件小事,但事实证明它不是(对我来说)。希望您能提供帮助。

I have a wpf window with two usercontrols inside of which the second is only shown when needed. I only set the MinWidth of the window in XAML, the MinHeight is provided through databinding an ist set depending on if the second usercontrol is shown. Now: How can I set the size of the window to different values than the MinWidth/Height during runtime. I tried setting the values before the Show(), after the Show(), in various events (Initialized, Loaded, etc.). I tried with and without UpdateLayout(), I tried setting the Height/Width through databinding. Nothing works! But when I debug the approaches I see that the Height/Width properties of the window are set to the expected values, but ActualHeight/Width stay. I thought it would be a bagatelle but it turned out it is not (to me). Thy for any help.

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

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

发布评论

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

评论(4

紙鸢 2024-12-06 22:56:05

您是否尝试过设置

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>

单击按钮设置窗口高度。这就是您要找的吗?

Have you tried to set

Application.Current.MainWindow.Height = 100;

Short addition: I just did a short test, in code:

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;
    }
}

and the 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>

Click on the Button sets the window height. Is that what you´re looking for?

酒儿 2024-12-06 22:56:05

你没有提供太多信息,所以我只是在这里猜测。

我可以重现窗口而不考虑其宽度和高度设置的唯一方法是将 SizeToContent 设置为 WidthAndHeight

You have no given much information, so I am just guessing here.

The only way I could reproduce the window not respecting its width and height settings was when I set SizeToContent to WidthAndHeight.

唔猫 2024-12-06 22:56:05

如果有人仍在为此苦苦挣扎,您唯一要做的就是以下内容:

如果您想调整主窗口的大小,只需编写以下代码。

Application.Current.MainWindow.Height = 420;

如果您想调整主窗口以外的新窗口的大小,只需在新窗口的 .cs 文件中编写以下代码即可。

Application.Current.MainWindow = this; 
Application.Current.MainWindow.Width = 420;

希望有帮助。

if someone is still struggling with this, the only thing you have to do is the following:

If you want to resize the main window just write the following code.

Application.Current.MainWindow.Height = 420;

If you want to resize a new window other than the main window just write the following code in the .cs file of the new window.

Application.Current.MainWindow = this; 
Application.Current.MainWindow.Width = 420;

Hope it helps.

沉溺在你眼里的海 2024-12-06 22:56:05

我只是简单地设置窗口高度属性。

最初 _myWindow.Height = 400;

一旦内容需要调整大小,我将其设置如下:

double newWindowHeight = a + b; 其中 a + b 使窗口变大,并且所以:

_myWindow.Height = newWindowHeight;

I just simply set the window Height property.

Originally _myWindow.Height = 400;

And once the content required resizing I set it as follows:

double newWindowHeight = a + b; Where a + b make the window larger, and so:

_myWindow.Height = newWindowHeight;

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