Search code examples
wpfxamlxamarindata-binding

XAML Data Binding Enabled from Function | Lock View Conditionally


Richt now I am checking in the code behind whether or not the controls in the view should be enabled:

public void Lock() {
    if (_status != 40 && _status != 60)
    {
        txt1.isEnabled = false;
        txt2.isEnabled = false;
        txt3.isEnabled = false;
    }
}

However, for pages with lot's of controls, this is a hassle. Is it possible to achieve the same with Data Binding? Something like this:

public void isEnabled() {
    if (_status != 40 && _status != 60)
        return false;
    else
        return true;
}

And then bind the controls to this function? (Sometimes the conditions for enabling/disabling a view are not as simple as in the example. It should be possible to do quite complex conditions.)

This program is developed with Xamarin. It should not make a difference for WPF.


Solution

  • create a property

    public bool isEnabled 
    {
        get 
        {
          if (_status != 40 && _status != 60)
              return false;
          else
              return true;
        }
    }
    

    if you want your UI to update dynamically, you will need to implement INotifyPropertyChanged and call PropertyChanged("isEnabled") whenever _status changes