Roy2511
Roy2511

Reputation: 1038

Is it possible to fire a converter if and only if some pre-condition is satisfied?

I have a group of radio buttons all connected up to the same variable in the viewmodel

<RadioButton Content="20" Margin="5,0" GroupName="open_rb" 
IsChecked="{Binding Path=mOpen.Threshold, Mode=OneWayToSource, Converter={StaticResource RadioBoolToIntConverter}, ConverterParameter=20, UpdateSourceTrigger=PropertyChanged}" />

<RadioButton Content="50" Margin="5,0" GroupName="open_rb"
  IsChecked="{Binding Path=mOpen.Threshold, Mode=OneWayToSource, Converter={StaticResource RadioBoolToIntConverter}, ConverterParameter=50, UpdateSourceTrigger=PropertyChanged}"/>

<RadioButton Content="70" Margin="5,0" GroupName="open_rb"
 IsChecked="{Binding Path=mOpen.Threshold, Mode=OneWayToSource, Converter={StaticResource RadioBoolToIntConverter}, ConverterParameter=70, UpdateSourceTrigger=PropertyChanged}"/>

And my converter is -

public class RadioBoolToIntConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();

        //should not hit this part
        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)
    {
        //bool to int (if bool is true then pass this val)
        bool val = (bool)value;
        if (val == true)
            return UInt32.Parse(parameter as string);

        else
        {
            //when you uncheck a radio button it fires through 
            //isChecked with a "false" value
            //I cannot delete this part, because then all code paths will not return a value
            return "";
        }
    }
}

Basically the idea is that if a certain radio button is clicked, the converter passes 20 or 30 or 70 (depends upon which radio button is clicked) to mOpen.Threshold which is an unsigned int.

Now, the radiobutton "isChecked" event gets triggered if the radio button is checked or not, with values of true and false. Now, if it's false, I return an empty string from the converter which doesn't parse to uint and causes the UI to complain.

Is it possible to only use this converter if the radio button is checked? Which implies that for this group, if I click a radio button, this converter should only be fired once, not twice.

Upvotes: 1

Views: 103

Answers (1)

mm8
mm8

Reputation: 169360

Using this approach it seems like you only want to set the source property when any of the IsChecked properties are set to true. You can then return Binding.DoNothing when val is false:

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    //bool to int (if bool is true then pass this val)
    bool val = (bool)value;
    if (val == true)
        return UInt32.Parse(parameter as string);

    return Binding.DoNothing;
}

Upvotes: 1

Related Questions