I have a class which has implemented INotifyPropertyChanged
. This class UserInfo
has a boolean variable isuserLoggedIn.
Now in my mainform I have buttons whose isEnabled I wish to bind to UserInfo.isuserLoggedIn
.
How to do that?
public class UserInfo : INotifyPropertyChanged
{
private static readonly UserInfo _instance = new UserInfo();
private string username;
private bool isLoggedIn;
public string UserName
{
get { return username; }
set
{
username = value;
NotifyPropertyChanged("UserName");
}
}
public bool UserLoggedIn
{
get { return isLoggedIn; }
set
{
isLoggedIn = value;
NotifyPropertyChanged("UserLoggedIn");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public static UserInfo GetUserInfo()
{
return _instance;
}
}
In the main I have:
public class MainWindow
{
UserInfo currentUser = UserInfo.GetUserInfo();
}
The XAML is:
<Button IsEnabled="{Binding ElementName=currentUser, Path=UserLoggedIn}"/>
You'll need to set the DataContext of your view to an instance of your UserInfo class. And then bind the IsEnabled property of your button to the UserIsLoggedIn boolean property on your UserInfo view model. Here's an example of binding an element's attribute to a property on a corresponding view model: passing a gridview selected item value to a different ViewModel of different Usercontrol
After seeing your edit, you'll again need to set the DataContext of your view to the currentUser object, then remove the ElementName portion of your button's IsEnabled binding expression.