I’m using a boolean converter in my XAML in a WPF project. I want to disable a few buttons while “IsBusy” is true. I am absolutely sure that IsBusy is being set to true/false properly. I am able to successfully bind directly to IsBusy without the converter. The following does not currently work. I’ve put breakpoints in the actual converter class and the “Convert” and “ConvertBack” methods are never hit. What is wrong here?
IsEnabled="{Binding IsBusy, Converter={StaticResource InvertedBooleanConverter}}"
Resources:
<Window.Resources>
<converters:InvertedBooleanConverter x:Key="InvertedBooleanConverter" />
</Window.Resources>
Converters:
xmlns:converters="clr-namespace:MyProject.Converters"
The converter:
namespace MyProject.Converters
{
[ValueConversion(typeof(bool), typeof(bool))]
public class InvertedBooleanConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (targetType != typeof(bool))
throw new InvalidOperationException("The target must be a boolean");
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
}
}
Take a step back and check that the binding is in the right place — add
<TextBlock Text="{Binding}" />and make sure it shows the correct class (the one that contains IsBusy).