簡體   English   中英

在 MVVM 中以編程方式更改 WPF 窗口大小

[英]Change WPF window Size programmatically in MVVM

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

我正在將窗口高度從 XAML 設置為 400,然后單擊表單上的按鈕試圖將高度增加到 500。

在我的按鈕的 ICommand 中,我正在使用:

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>

請注意,結合Window.Width不夠這個工作。

我對此有類似的要求,另外我想跟蹤一些其他窗口設置。 所以,我有這門課:

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

例如,如果我想以編程方式更新寬度,我會這樣做:

WindowSettings.Width = 456;
RaisePropertyChangedEvent("WindowSettings.Width");

(當然,我的視圖模型繼承自實現 INotifyPropertyChanged 的​​基類。)

我想在 WindowSettings 類上提供一個事件,以便對屬性的更新可以自動導致屬性更改通知,但我的要求是跟蹤窗口大小並在啟動時設置它,所以我沒有打擾. 希望有幫助。

使用 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>

而是將Mode=TwoWay添加到您的綁定中。

<Window Width="{Binging Width, Mode=TwoWay}">
    ...
</Window>

您確定要調整大小的窗口是 MainWindow 嗎? 或者您確定您的命令已成功綁定到您的按鈕上? 你可以試試下面的代碼:

看法:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewModel="clr-namespace:WpfApplication1.ViewModel"
        Title="Wpf Application" Height="300" Width="400"
        WindowStartupLocation="CenterScreen">
    <Window.DataContext>
        <viewModel:MainWindowViewModel />
    </Window.DataContext>
    <Grid>
        <Button Content="Resize" Command="{Binding ResizeCommand}" IsEnabled="{Binding ResizeCommand.CanExecute}" />
    </Grid>
</Window>

視圖模型:

public class MainWindowViewModel
{
    private RelayCommand _resizeCommand;

    public RelayCommand ResizeCommand { get { return _resizeCommand; } }

    public MainWindowViewModel()
    {
        this._resizeCommand = new RelayCommand(this.Resize);
    }

    private void Resize()
    {
        Application.Current.MainWindow.Height = 500;
    }
}

這個作品

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM