Binding button IsEnabled to a property

12,720

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.

Share:
12,720
xaria
Author by

xaria

Updated on June 04, 2022

Comments

  • xaria
    xaria almost 2 years

    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}"/>
    
  • xaria
    xaria about 12 years
    But I have already declared an instance of the UserInfo class in the MainWindow UserInfo currentUser = UserInfo.GetUserInfo(); and the Button has binding as follows: <Button IsEnabled="{Binding ElementName=currentUser, Path=UserLoggedIn}"/> Why do I still need to set the DataContext?
  • KodeKreachor
    KodeKreachor about 12 years
    I think you have a slight misunderstanding of how data binding works in Silverlight and WPF. Your binding expressions are going to look for a matching property on the data context of the view. Element binding refers to binding the value of one XAML element to an attribute of another XAML element.