TransWikia.com

WPF c# ViewModel

Stack Overflow на русском Asked by VeNNoM on November 22, 2021

Есть 2 юзер контрола: Логин панель и панель регистрации. У них есть свои ViewModel’и. Есть еще AuthViewModel и BaseViewModel, код:

BaseViewModel

public class BaseViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string prop = "")
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
        public static LoginViewModel L_GUI = new LoginViewModel();
        public static RegistrationViewModel R_GUI = new RegistrationViewModel();

        private BaseViewModel _selectedViewModel;
        public BaseViewModel SelectedViewModel
        {
            get { return _selectedViewModel; }
            set
            {
                _selectedViewModel = value;
                OnPropertyChanged("SelectedViewModel");
            }
        }
    }

AuthViewModel

public class AuthViewModel : BaseViewModel
    {
        public AuthViewModel()
        {
            SelectedViewModel = L_GUI;
        }

        private RelayCommand addCommand;
        public RelayCommand Registration
        {
            get
            {
                return addCommand ??
                    (addCommand = new RelayCommand(obj =>
                    {
                        SelectedViewModel = R_GUI;
                    }));
            }
        }


        private RelayCommand logincommand;
        public RelayCommand LogIn
        {
            get
            {
                return logincommand ??
                    (logincommand = new RelayCommand(obj =>
                    {
                        SelectedViewModel = L_GUI;
                    }));
            }
        }
    }

LoginViewModel

public class LoginViewModel : BaseViewModel
    {
        private RelayCommand logincommand;
        public RelayCommand Registration
        {
            get
            {
                return logincommand ??
                    (logincommand = new RelayCommand(obj =>
                    {
                        SelectedViewModel = R_GUI;
                    }));
            }
        }
    }

RegistrationViewModel

 public class RegistrationViewModel : BaseViewModel
    {
        private RelayCommand logincommand;
        public RelayCommand LogIn
        {
            get
            {
                return logincommand ??
                    (logincommand = new RelayCommand(obj =>
                    {
                        SelectedViewModel = R_GUI;
                    }));
            }
        }
    }

Свойство SelectedViewModel определено в BaseViewModel, он отвечает за выбранный ViewModel. При смене ViewModel, сменяется и View. Так вот, при изменении свйоства SelectedViewModel из класса LoginViewModel и RegistrationViewModel само свойство вроде как меняется, но View не меняется. Я может не очень объяснил, могу скинуть проект.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP