6

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.

2
  • Unless you assigned the data context to the control then IsLoggedIn must be a property in the View Model, not in the View code behind (and if your data context is the View then revert it now...) Commented May 14, 2018 at 13:29
  • 1
    If you change IsLoggedIn after the GUI is loaded, you need to let the WPF system know that the value has changed. Either declare IsLoggedIn as DependencyProperty or implement INotifyPropertyChanged
    – Freggar
    Commented May 14, 2018 at 13:33

2 Answers 2

8

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));
        }
    }
6
  • 1
    Assign an instance of Model to the DataContext property of your MainWindow. E.g. add a field like private Model viewModel = new Model();, and add DataContext = viewModel; in the MainWindow constructor.
    – Clemens
    Commented May 14, 2018 at 13:57
  • Well, it should work. Make the enabled field private to avoid that it is accidentially accessed from outside the Model class.
    – Clemens
    Commented May 14, 2018 at 14:05
  • @Clemens, thanks, it really helps when I set IsEnabled="{Binding Path = IsLoggedIn}", but not working IsEnabled="{Binding Path = !IsLoggedIn} for another button, which should have the opposite property to login button. Commented May 14, 2018 at 14:09
  • 2
    !IsLoggedIn is not a valid property path in WPF data binding. Either add another property, or write a Binding Converter that inverts the boolean value.
    – Clemens
    Commented May 14, 2018 at 14:10
  • 2
    Upvoted because this view model is pretty much how it's done. By the way, the CallerMemberName attribute means you can just write OnPropertyChanged() here. For inversion, you might as well follow the advice from Clemens and make a value converter. You're likely to need one to invert booleans again (this has been my experience at least).
    – Mikkel K.
    Commented May 14, 2018 at 14:15
5

Two things are missing:

  1. The IsLoggedIn property should be in DataContext object. In MVVM, this means it should be in the view model.
  2. The DataContext should implement INotifyPropertyChanged so the view can change when you update the property programatically.
2
  • I can't really commit to a longer answer at the moment, things are happening here.
    – Mikkel K.
    Commented May 14, 2018 at 13:55
  • The OP has pretty much answered it now, I think I'll upvote that instead.
    – Mikkel K.
    Commented May 14, 2018 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.