I have a radio button Boolean to integer converter class:
public class RadioBoolToIntConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
int integer = (int)value;
if (integer == int.Parse(parameter.ToString()))
return true;
else
return false;
}
public object ConvertBack(object value, Type targetType, object parameter,
CultureInfo culture)
{
return parameter;
}
}
I have two radio buttons inside a stackpanel bound to a property "TestTypeRef":
<StackPanel Orientation="Horizontal">
<RadioButton
Content="Screening"
Margin="0 0 10 0"
IsChecked="{Binding Path=TestTypeRef, Mode=TwoWay, Converter={StaticResource RadioBoolToIntConverter}, ConverterParameter=0 }" />
<RadioButton
Content="Full"
Margin="0 0 10 0"
IsChecked="{Binding Path=TestTypeRef, Mode=TwoWay, Converter={StaticResource RadioBoolToIntConverter}, ConverterParameter=1 }" />
</StackPanel>
The problem arises when I try to set the value of the property to which the radiobuttons are bound, in the associated ViewModel.
When I set the value from 0 to 1 - this is fine.
When I set the value from 1 to 0 - the property value remains 1:
Inside the ViewModel:
// Set the default test type.
TestTypeRef = 0;
// ~~> TestTypeRef = 0
TestTypeRef = 1;
// ~~> TestTypeRef = 1
TestTypeRef = 0;
// ~~> TestTypeRef = 1 i.e. no change.
Many thanks for any help you can offer.
Update: Thank you to both @Rachel and @Will for their feedback. The ConvertBack routine was in error. This fixed it:
return value.Equals(false) ? DependencyProperty.UnsetValue : parameter;