I have a group of ribbon toggle buttons inside of the same container in my XAML like this:
<ribbon:RibbonGroup Header="Layouts">
<ribbon:RibbonToggleButton Label="One"
IsChecked="{Binding PaneManager.Layout,
Converter={StaticResource EnumToBooleanConverter},
ConverterParameter={s:Static windows:Layouts.One}}"/>
<ribbon:RibbonToggleButton Label="Two Vertical"
IsChecked="{Binding PaneManager.Layout,
Converter={StaticResource EnumToBooleanConverter},
ConverterParameter={s:Static windows:Layouts.TwoVertical}}"/>
<!-- etc. for 2 horizontal and 4 panes -->
</ribbon:RibbonGroup>
I'm using the same EnumToBooleanConverter outlined in this answer:
public class EnumToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(true) ? parameter : Binding.DoNothing;
}
}
The problem is that when a user clicks the toggle button that's already selected, the toggle button happily turns itself off--even though the bound value matches that toggle button. When tracing through the code, what happens is that the ConvertBack
method is called and it returns Binding.DoNothing
. Afterwards, Convert
is not called to reassign the value. When ConvertBack
returns a value (i.e. it is clicked a second time and IsChecked
is true again), the Convert
method is called to reassign the value.
If I change the return type on false to be DependencyObject.UnsetValue
, the toggle button is still turned off but now it has a red outline.
How do I force WPF to re-evaluate the bound value so that the toggle button stays on?