Try to remove the DataContext = new UserViewModel()
lines from signinUC and resetPwdUC.
How to control the Visibility property of a UserControl object dynamically using Binding property?
I am working on a .NET C# project to learn MVVM.
I am struggling to control the Visibility property of a UserControl dynamically.
The login screen (userView.xaml) is a small centralized box that has two userControl views named signInUC.xaml
and resetPwdUC.xaml
Initially it displays signInUC.xaml that has got two fields username and password. When user inputs their credentials - user is authenticated. If user has got a default password (which is checked in the back end), I then display change password screen (resetPwdUC.xaml
) where user will change the password. Again, I display signInUC.xaml
to login with new password.
I intend to do this by controlling the Visibility of UserControl whenever the backend logic returns true.
Here is my Code:
UserView.xaml
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Sample.views"
xmlns:controls="clr-namespace:Sample.controls" x:Class="Sample.views.UserView"
mc:Ignorable="d"
d:DesignHeight="640" d:DesignWidth="1120">
<Grid x:Name="mainGrid" Background="#FFFDFDFD">
<controls:signinUC Visibility="{Binding SignInView}" />
<controls:resetPwdUC Visibility="{Binding ResetPwdView}" />
</Grid>
</UserControl>
UserView.xaml.cs
public partial class UserView : UserControl
{
public UserView()
{
DataContext = new UserViewModel();
InitializeComponent();
}
}
UserViewModel.cs
public class UserViewModel : INotifyPropertyChanged
{
private Visibility _signin = Visibility.Hidden;
public Visibility SignInView
{
get {
return _signin;
}
set
{
_signin = value;
OnPropertyChanged("SignInView");
}
}
private Visibility _resetPwd = Visibility.Hidden;
public Visibility ResetPwdView
{
get {
return _resetPwd;
}
set
{
_resetPwd = value;
OnPropertyChanged("ResetPwdView");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string a_PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(a_PropertyName));
}
}
public UserViewModel()
{
Credentials = new UserModel();
/* Initially displaying Sign in view */
SignInView = Visibility.Visible;
ResetPwdView = Visibility.Hidden;
}
private void SignIn_Click()
{
/* ......Sign in logic here... */
if(isAuthenticated)
{
SignInView = Visibility.Hidden;
ResetPwdView = Visibility.Visible;
}
// Here the SignInView is hidden which is working as expected but ResetPwdView is not Visible however I want this to be visible here.
// Did Console.WriteLine(ResetPwdView) and I got the result as Visible but its not displayed.
}
}
SignInView.xaml.cs
public partial class SignInView : UserControl
{
public SignInView()
{
DataContext = new UserViewModel();
InitializeComponent();
}
private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
{
var viewModel = this.DataContext as UserViewModel;
if (viewModel != null) {
viewModel.Credentials.PasswordInput = new NetworkCredential("", passwordTextBox.Password).SecurePassword;
}
}
}
The problem I am facing is - I am able to hide the signInUC.xaml
after login with default password however I am not able to display resetPwdUC.xaml
Am I missing something? Thanks in Adavance.
Windows Presentation Foundation
-
Viorel 121.6K Reputation points
2025-03-27T19:51:12.3166667+00:00