Binding the “WindowState” property of a window in WPF using MVVM

后端 未结 3 1030
盖世英雄少女心
盖世英雄少女心 2021-01-12 06:01

I bound the \"WindowState\" property of my main window to my ViewModel in order to change the state of the window by a command, but the first time I minimize the window it m

3条回答
  •  时光说笑
    2021-01-12 06:25

    this is a sample work around that tested with Relaying Command Logic. You will get more detail on WPF Apps With The Model-View-ViewModel Design Pattern .

    
        
            
        
        
            
            
            
        
    
    

    and in the 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");
                }
            }
        }
    

提交回复
热议问题