In WPF typical way for handling this would be to use data binding with value convertor
Here is a quick look at the changes you will need to do:
In your xaml you will bind your control's IsEnabled property to Floor
<Button Content="Click Me!" Click="button_Click"
x:Name="permit1" IsEnabled="{Binding Floor, Converter={StaticResource converter}}">
Floor is an int, while IsEnabled is the bool, to link these you need the converter, I have put in the converter as a windows resources so that it can be used for multiple different objects (in the same class, but you can put it in a central resource.xaml file).
<Window.Resources>
<local:FloorToEnableConverter x:Key="converter" />
</Window.Resources>
For the framework to be able to find your property Floor it should be in the datacontext of the XAML, I have done this in the code behind constructor
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
You will need to define the logic for this converter in your own class, which is just the implementation of IValueConverter interface
public class FloorToEnableConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
throw new ArgumentException("value cannot be null");
if ((int)value == 1)
return false;
if ((int)value == 2)
return true;
// for all other values disable
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Finally when the property changes you will need to notify the UI about the changes, so this needs to be implemented by INotifyPropertyChangeInterface
I have implemented this on my demo class
public partial class MainWindow : Window, INotifyPropertyChanged
And the actual code is:
public event PropertyChangedEventHandler PropertyChanged;
public int floor;
public int Floor
{
get { return this.floor;}
set { this.floor = value;
// The string here should exactly match the property name
OnPropertyChanged("Floor"); }
}
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
This should work, also even though in this example I have implemented the code for change notification in the code behind class, in WPF try to avoid using the code behind class, that was a required in win forms, but WPF provides alternate mechanism for this. Have a look at the MVVM pattern for a better way to structure the code.
if/elsestatement, orpermit1.IsEnabled = (floor == 2);.