C# 在MVVM中以编程方式更改WPF窗口大小

C# 在MVVM中以编程方式更改WPF窗口大小,c#,wpf,mvvm,C#,Wpf,Mvvm,如何使用MVVM方法以编程方式更改WPF中窗口的大小 我正在将窗口高度从XAML设置为400,然后单击表单上的按钮尝试将高度增加到500 在我的按钮图标中,我正在使用: Application.Current.MainWindow.Height = 500; 但是它没有做任何事情。尝试在MainWindow.xaml.cs文件中的Loaded事件中设置“Application.Current.MainWindow”属性: private void MainWindow_Loaded(objec

如何使用MVVM方法以编程方式更改WPF中窗口的大小

我正在将窗口高度从XAML设置为400,然后单击表单上的按钮尝试将高度增加到500

在我的按钮图标中,我正在使用:

Application.Current.MainWindow.Height = 500;

但是它没有做任何事情。

尝试在
MainWindow.xaml.cs
文件中的
Loaded
事件中设置“Application.Current.MainWindow”属性:

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    Application.Current.MainWindow = this;
}
更新>>>

我的朋友,请记住,你说过你想使用
Application.Current.MainWindow
。。。这就是你可以使用它的方式。但是,如果您想以MVVM的方式执行此操作,那么为什么不将该值绑定到
Window.Width
属性

<Window Width="{Binding Width}" MinWidth="{Binding Width}" MaxWidth="{Binding Width}">
    ...
</Window>

...

请注意,绑定到
窗口。宽度
不足以执行此操作。

您确定要调整大小的窗口是MainWindow吗?或者您确定您的命令已成功绑定到按钮上?您可以尝试以下代码:

视图:


我有一个类似的要求,除了我想跟踪一些其他窗口设置。所以,我有一节课:

public class WindowSettings
{
    public int Width { get; set; }

    public int Height { get; set; }

    public int Left { get; set; }

    public int Top { get; set; }
}
然后在我的视图模型中,我有:

public WindowSettings WindowSettings
{
    get
    {
        return _windowSettings;
    }
}
在xaml中,这是:

<Window ...
    Height="{Binding WindowSettings.Height,Mode=TwoWay}"
    Width="{Binding WindowSettings.Width, Mode=TwoWay}"
Left="{Binding WindowSettings.Left,Mode=TwoWay}"
    Top="{Binding WindowSettings.Top,Mode=TwoWay}">
(当然,我的视图模型继承自实现INotifyPropertyChanged的基类。)


我曾玩弄过在WindowsSettings类上提供一个事件的想法,这样对属性的更新就可以自动引起属性更改通知,但我的要求是跟踪窗口大小并在启动时设置它,所以我没有费心。希望能有所帮助。

使用VS 2017和Caliburn Micro,我对设置宽度没有问题。你这样做的方式不是MVVM,你的VM不需要知道V或者当int Width改变时会发生什么

public class ShellViewModel : Caliburn.Micro.PropertyChangedBase, IShell
{
    public ShellViewModel()
    {
        Width = 500;
    }
    private int _width;
    public int Width
    {
        get => _width;
        set
        {
            _width = value;
            NotifyOfPropertyChange(() => Width);
        }
    }
    public void MyButton()
    {
        Width = 800;
    }

}
窗口中的XAML

Width="{Binding Width, Mode=TwoWay}"
在网格中

<Button x:Name="MyButton" Content="Make 800 wide" />


它在500打开,当我点击按钮时,它会延伸到800。

我添加这个答案,因为投票最多的答案实际上是不正确的。绑定到MinHeight和MaxHeight将导致无法使用夹点调整窗口大小

<Window Width="{Binding Width}" MinWidth="{Binding Width}" MaxWidth="{Binding Width}">
    ...
</Window>
这很有效

Application.Current.MainWindow = this;
Application.Current.MainWindow.WindowState = WindowState.Normal;
Application.Current.MainWindow.Width = 800;
Application.Current.MainWindow.Height = 450;

MVVM中没有任何内容表明您不能使用代码隐藏。@user2519971实际上,在这种情况下使用代码隐藏是正确的。在MVVM的上下文中,ViewModel不应该知道视图。所以,在虚拟机中操纵视图是一种违背MVVM目的的糟糕做法。@Sheridan。你是对的。我尝试设置windowwidth=“{Binding Width}”,但它不起作用。这有什么问题吗?为什么
Window.Width
不够?对不起,我没有写框架,所以我真的猜不出他们为什么要这样做。。。这可能是一个错误,一个疏忽,或者他们出于某种未知的原因这样做。嗨,CodeGen,谢谢你的回答。这个代码块可以解决这个问题,但对其他人没有多大帮助。考虑一下为什么要解决ASKER的问题,为什么他们尝试的方法不起作用;否则,他们可能会再次犯同样的错误。这个答案可能是正确的,但请解释它是如何工作的。这样,它可以帮助其他跟踪qtn的人
<Button x:Name="MyButton" Content="Make 800 wide" />
<Window Width="{Binding Width}" MinWidth="{Binding Width}" MaxWidth="{Binding Width}">
    ...
</Window>
<Window Width="{Binging Width, Mode=TwoWay}">
    ...
</Window>
Application.Current.MainWindow = this;
Application.Current.MainWindow.WindowState = WindowState.Normal;
Application.Current.MainWindow.Width = 800;
Application.Current.MainWindow.Height = 450;