I'm trying to accomplish having a couple of ToggleButtons act sort of like radio buttons--but with the important difference being that having neither checked is a valid case (only up to one can be checked at a time, they are mutually-exclusive).
Here is some XAML that almost works:
Clicking on one button will indeed uncheck the other. Unfortunately what is also happening is that the button clicked does not get checked. The background changes properly when the mouse is moved over it, but when the mouse moves away the button shows as not selected.
Commenting-out the Setters allows the clicked button to be checked. It's as if the Setter is also unchecking the clicked button.
I would rather handle this in XAML than have to resort to implementing in code via event handlers; this would keep everything defined in one place.
Ideas?
TIA!
Siddhartha SarkarPosted Apr 30, 2014, 7:36 AM
I am afraid it will be difficult to achieve in xaml alone. To handle the case of both buttons remaining unchecked is sort of not possible in xaml alone because whatever triggers/binding you are going to use are going to to evaluated at the time of the control load and state of one button will affect the state of another. To handle this, you will need two independent bool properties in ViewModel and on change of the properties you can change the other. However, if you are ok with one checked ALWAYS, then it can be achieved in xaml fairly easily:
IsChecked="{Binding ElementName=ShowDotGridToggleButton, Path=IsChecked, Mode=TwoWay, Converter={StaticResource inverseBoolConv}}"/>
And here is the converter:
public class InverseBoolConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool) value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)value;
}
}