Binding WindowState property of a window in WPF using MVVM

I linked the WindowState property of my main window to my ViewModel to change the state of the window with the command, but for the first time I will minimize the window that it minimizes, like a sheet in an Excel file. Is there any work for this or the right way to bind the WindowState property to my ViewModel so that the window minimizes correctly?

+5
source share
3 answers

I don’t think that you should take care of the state of the window in the view model, this is completely wrong, because the lower level layer knows the higher level layer (thus, incorrect separation of problems (SOC)).

, , - ( , ), . , (, , - , unit test).

+6

, Relaying Command Logic. WPF Model-View-ViewModel.

<Window x:Class="WpfMvvmTestCSharp.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:WpfMvvmTestCSharp"
    Title="Window1" Height="300" Width="300" WindowState="{Binding CurWindowState, Mode=TwoWay}">
    <Window.DataContext>
        <vm:Window1ViewModel/>
    </Window.DataContext>
    <Grid>
        <Button Command="{Binding CmdMax}" Height="23" Margin="12,25,0,0" Name="button1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="75">Maximize</Button>
        <Button Command="{Binding CmdMin}" Height="23" Margin="101,25,102,0" Name="button2" VerticalAlignment="Top">Minimize</Button>
        <Button Command="{Binding CmdRes}" Height="23" HorizontalAlignment="Right" Margin="0,25,13,0" Name="button3" VerticalAlignment="Top" Width="75">Restore</Button>
    </Grid>
</Window>

Windows ViewModel

class Window1ViewModel:ViewModelBase
    {
        public Window1ViewModel()
        {
            CurWindowState = WindowState.Maximized;
        }

        public ICommand CmdMax
        {
            get { return new RelayCommand(param => onCmdMax()); }
        }

        void onCmdMax()
        {
            CurWindowState = WindowState.Maximized;
        }
        public ICommand CmdMin
        {
            get { return new RelayCommand(param => onCmdMin()); }
        }
        void onCmdMin()
        {
            CurWindowState = WindowState.Minimized;
        }
        public ICommand CmdRes
        {
            get { return new RelayCommand(param => onCmdRes()); }
        }

        void onCmdRes()
        {
            CurWindowState = WindowState.Normal;
        }

        private WindowState _curWindowState;
        public WindowState CurWindowState
        {
            get
            {
                return _curWindowState;
            }
            set
            {
                _curWindowState = value;
                base.OnPropertyChanged("CurWindowState");
            }
        }
    }
+13

, , AND, , , :

    <Button Command="{Binding SnoozeCommand}" Click="Button_Click">Snooze</Button>

The command in this case affects the virtual machine. The Click event only changes the state of the window.

0
source

All Articles