Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind IsEnabled property for Button in WPF

Tags:

c#

binding

wpf

I have a Button which needs to be enabled/disabled programmatically. I want to achieve this using a binding to a bool. Here is the Button XAML:

<Button x:Name="logInButton" Height="30" IsEnabled="{Binding IsLoggedIn}">
                            <Image Source="/images/img.png"></Image>
                        </Button>

Here is the code being called:

        public MainWindow()
        {
            InitializeComponent();
            enabled = false;
        }
        private bool enabled;
        public bool IsLoggedIn
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
            }
        } 

The value of the property IsLoggedIn is assigned correctly. But IsEnabled is not assigned the value I need. For example:
For example

I tried setting the value with Binding Path and Binding Source but nothing is working.

Please advise what may be wrong.

like image 600
Hanna Bilous Avatar asked May 14 '18 13:05

Hanna Bilous


1 Answers

Then... I think must be so.

class Model : INotifyPropertyChanged
    {
        public bool enabled;
        public bool IsLoggedIn
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
                OnPropertyChanged("IsLoggedIn");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName]string property = "")
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
like image 50
Hanna Bilous Avatar answered Nov 11 '22 14:11

Hanna Bilous