我有一个已经实现的类INotifyPropertyChanged
。这个类UserInfo
有一个布尔变量 isuserLoggedIn。现在在我的主窗体中,我有希望绑定到其 isEnabled 的按钮UserInfo.isuserLoggedIn
。
怎么做?
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;
}
}
我主要有:
public class MainWindow
{
UserInfo currentUser = UserInfo.GetUserInfo();
}
XAML 是:
<Button IsEnabled="{Binding ElementName=currentUser, Path=UserLoggedIn}"/>