I have two buttons in my application
Now I want to bind the btnOff to !isOn. Meaning is btnOn is Enabled, btnOff should be Disabled and vice-versa
EDIT: Below is my implementation:
<Button x:Name="btnOn" Content="On" Width="45" Height="24" IsEnabled="{Binding isOn, Converter={StaticResource BoolInverter}}" />
<Button x:Name="btnOff" Content="Off" Width="45" Height="24" IsEnabled="{Binding isOn} />
public class BoolInverterConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool)
{
return !(bool)value;
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool)
{
return !(bool)value;
}
return value;
}
}
Wouldn’t it be simpler to have another computed/derived property IsOff (no backing field) that negates the IsOn property ?
Converters are normally used to convert data types e.g. to bind a Visibility enum property to a boolean backing property in the viewmodel.