Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't change views on WPF C#

I’m having a problem while doing a project on WPF C# where depending on the type user that gets it from a table in SQL Server changes de type of the view of my project.

For example, if the type user is 1 (client) the view will be ControlPanelViewCommand, if it’s 3 will be AdminControlPanelViewCommand, I’ll show u what I’ve been trying to do for the last 2 hours and it kinda works but when I change the view back it doesn’t come back

After opening the program and doing the login:
After opening the program and doing the login
After changing view and going back to the first one:
After changing view and going back to the first one

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Code when the combo box is clicked:

// "PRIVILEGES" OF EACH TYPE OF USER
            if (Convert.ToInt32(typeUser) == 1)
            {
                ManageReservationsRadionButton.Visibility = Visibility.Collapsed;
                ManageRequestsRadioButton.Visibility = Visibility.Collapsed;
                ManageUsersRadionButton.Visibility = Visibility.Collapsed;
                LogoutRadioButton.Margin = new Thickness(0, 490, 0, 0);
                ControlPanelRadioButton.IsChecked = true;
                //ControlPanelRadioButton.Command = ;
            }
            else if (Convert.ToInt32(typeUser) == 2)
            {
                ControlPanelRadioButton.Visibility = Visibility.Collapsed;
                ManageUsersRadionButton.Visibility = Visibility.Collapsed;
                RequestsRadioButton.Visibility = Visibility.Collapsed;
                LogoutRadioButton.Margin = new Thickness(0, 490, 0, 0);
                ManageReservationsRadionButton.IsChecked = true;
            }
            else
            {
                RequestsRadioButton.Visibility = Visibility.Collapsed;
                LogoutRadioButton.Margin = new Thickness(0, 350, 0, 0);
                ControlPanelRadioButton.IsChecked = true;
                //ControlPanelRadioButton.Command = ;
            }
<RadioButton Content="Painel de Controlo"
                             Height="50"
                             Foreground="White"
                             Margin="0,20,0,0"
                             Style="{StaticResource MenuButtonTheme}"
                             Command="{Binding ControlPanelViewCommand}"
                             x:Name="ControlPanelRadioButton"/>~
<DataTemplate DataType="{x:Type viewModel:ControlPanelViewModel}">
                <view:ControlPanel />
            </DataTemplate>

            <DataTemplate DataType="{x:Type viewModel:AdminControlPanelViewModel}">
                <view:AdminControlPanel />
            </DataTemplate>
class MainViewModel : ObservableObject
    {
        // CONNECTION
        SqlConnection con = new SqlConnection("Data Source=BAGACINHO;Initial Catalog=reservas_PAP;Integrated Security=True");

        // VARIABLES
        string data, typeUser;

        // ------------------------------------ RelayCommand ---------------------------------------- //

        public RelayCommand ControlPanelViewCommand { get; set; }

        public RelayCommand AdminControlPanelViewCommand { get; set; }

        public RelayCommand RequestsViewCommand { get; set; }

        public RelayCommand ManageReservationsViewCommand { get; set; }

        public RelayCommand ManageRequestsViewCommand { get; set; }

        public RelayCommand ManageUsersViewCommand { get; set; }

        // ------------------------------------- ViewModel ----------------------------------------- //

        public ControlPanelViewModel ControlPanelVM { get; set; }

        public AdminControlPanelViewModel AdminControlPanelVM { get; set; }

        public RequestsViewModel RequestsVM { get; set; }

        public ManageReservationsViewModel ManageReservationsVM { get; set; }

        public ManageRequestsViewModel ManageRequestsVM { get; set; }

        public ManageUsersViewModel ManageUsersVM { get; set; }

        // ----------------------------------- CurrentView ---------------------------------------- //

        private object _currentView;

        public object CurrentView
        {
            get { return _currentView; }
            set
            {
                _currentView = value;
                OnPropertyChanged();
            }
        }

        public MainViewModel()
        {
            // CALLS OF THE VIEWMODELS
            ControlPanelVM = new ControlPanelViewModel();
            AdminControlPanelVM = new AdminControlPanelViewModel();
            RequestsVM = new RequestsViewModel();
            ManageReservationsVM = new ManageReservationsViewModel();
            ManageRequestsVM = new ManageRequestsViewModel();
            ManageUsersVM = new ManageUsersViewModel();

            // OPEN CONNECTION
            con.Open();

            // SQL QUERY
            data = "SELECT type_user FROM Users WHERE username = @user";

            using (SqlCommand cmd = new SqlCommand(data, con))
            {
                cmd.Parameters.AddWithValue("@user", Settings.Default.n_cliente);

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        typeUser = reader["type_user"].ToString();
                    }
                }
            }

            // CLOSE CONNECTION
            con.Close();

            // CURRENT VIEW OF THE TYPE OF THE USER
            if (Convert.ToInt32(typeUser) == 1)
            {
                CurrentView = ControlPanelVM;
            }
            else if (Convert.ToInt32(typeUser) == 2)
            {
                CurrentView = ManageReservationsVM;
            }
            else
            {
                CurrentView = AdminControlPanelVM;
            }

            // ----------------------------------- ViewCommand ---------------------------------------- //
            ControlPanelViewCommand = new RelayCommand(o => { CurrentView = ControlPanelVM; });

            AdminControlPanelViewCommand = new RelayCommand(o => { CurrentView = AdminControlPanelVM; });

            RequestsViewCommand = new RelayCommand(o => { CurrentView = RequestsVM; });

            ManageReservationsViewCommand = new RelayCommand(o => { CurrentView = ManageReservationsVM; });

            ManageRequestsViewCommand = new RelayCommand(o => { CurrentView = ManageRequestsVM; });

            ManageUsersViewCommand = new RelayCommand(o => { CurrentView = ManageUsersVM; });
        }
    }

There’s all the code supposedly is needed for it, and I want to understand why isn’t it working.

Tried doing some research but couldn’t find anything useful

>Solution :

You need to just show/assign the control you want to see

 if (Convert.ToInt32(typeUser) == 1)
 {
      CurrentView = ControlPanelVM;
      ManageReservationsRadionButton.Visibility = Visibility.Collapsed;
      ManageRequestsRadioButton.Visibility = Visibility.Collapsed;
      ManageUsersRadionButton.Visibility = Visibility.Collapsed;
      LogoutRadioButton.Margin = new Thickness(0, 490, 0, 0);
      ControlPanelRadioButton.IsChecked = true;
      //ControlPanelRadioButton.Command = ;
 }
        

 else if (Convert.ToInt32(typeUser) == 2)
 {
    CurrentView = ManageReservationsVM;
    ControlPanelRadioButton.Visibility = Visibility.Collapsed;
    ManageUsersRadionButton.Visibility = Visibility.Collapsed;
    RequestsRadioButton.Visibility = Visibility.Collapsed;
    LogoutRadioButton.Margin = new Thickness(0, 490, 0, 0);
    ManageReservationsRadionButton.IsChecked = true;
 }
 else
 {
    CurrentView = AdminControlPanelVM;
    RequestsRadioButton.Visibility = Visibility.Collapsed;
    LogoutRadioButton.Margin = new Thickness(0, 350, 0, 0);
    ControlPanelRadioButton.IsChecked = true;
    //ControlPanelRadioButton.Command = ;
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading