2012-04-05 7 views
2

私はINotifyPropertyChangedを実装したクラスを持っています。このクラスUserInfoには、boolean変数isuserLoggedInがあります。 私のメインフォームには、ボタンがあります。そのボタンはisEnabledで、UserInfo.isuserLoggedInにバインドします。バインディングボタンIsEnabledプロパティに

どうすればよいですか?私はメインで

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}"/> 

答えて

2

あなたののUserInfoクラスのインスタンスにビューのDataContextのを設定する必要があります。次に、ボタンのIsEnabledプロパティをUserInfoビューモデルのUserIsLoggedInブール値プロパティにバインドします。次に、対応するビューモデルのプロパティに要素の属性をバインドする例を示します。passing a gridview selected item value to a different ViewModel of different Usercontrol

編集内容を確認した後、ビューのDataContextをcurrentUserオブジェクトに設定し、次にElementName部分を削除する必要があります。ボタンのIsEnabledバインディング式。

+0

しかし、私はすでにMainIndexのUserInfoクラスのインスタンスを宣言しています。UserInfo = UserInfo.GetUserInfo();ボタンは次のようにバインドされています。

+0

SilverlightとWPFでデータバインディングがどのように機能するかを少し誤解していると思います。バインディング式は、ビューのデータコンテキストで一致するプロパティを探します。要素のバインディングとは、あるXAML要素の値を別のXAML要素の属性にバインドすることを指します。 – KodeKreachor

関連する問題