3

I got a strange behaviour if I want to update my UI by RaisePropertyChanged. I use the 2nd solution (by Johnathan1) of this post: I implemented the RadioBoolToIntConverter.

My VM looks like this:

public int myFilterRadioButtonInt
        {
            get
            {
                return _Filter.FilterMyProperty ? 1 : 2;
            }
            set
            {
                if (value == 1)
                    _Filter.FilterMyProperty = true;
                else if (value == 2)
                    _Filter.FilterMyProperty = false;
                else
                    return;

                RaisePropertyChanged("myFilterRadioButtonInt");
            }
        }

Converter looks like this (by Jonathan1 of this post):

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;
        }
    }

For understanding _Filter.FilterMyProperty is a bool value of the model which is responsible if my value which will be filtered is shown or is not shown. This is binded to 2 RadioButtons using the RadioBoolToIntConverter:

<RadioButton IsChecked="{Binding Path=myFilterRadioButtonInt, Converter={StaticResource RadioBoolToIntConverter}, ConverterParameter=1}">Show</RadioButton>
 <RadioButton IsChecked="{Binding Path=myFilterRadioButtonInt, Converter={StaticResource RadioBoolToIntConverter}, ConverterParameter=2}">Don't show</RadioButton>

Binding and toggling the RadioButtons is working properly.

The problem is if I set _Filter.FilterMyProperty = true by code (set a standard filter where this value should be filtered) and then do RaisePropertyChanged("myFilterRadioButtonInt") the _Filter.FilterMyProperty will be set to false.

Edit:

By RaisePropertyChanged("myFilterRadioButtonInt") (called by the setter of the Filter Property in the VM) the setter of myFilterRadioButtonInt is called again and it will set the current value of the RadioBox (in my case the value is 2 so the setter will set back the _Filter.FilterMyProperty to false.

It is not possible to change the value of the RadioBoxes by code with this method. I thought only the getter will be called when I call RaisePropertyChanged("myFilterRadioButtonInt"). How can I solve this problem and why is the setter being called by RaisePropertyChanged("myFilterRadioButtonInt")?

Edit:

Returning the parameter in ConvertBack() was the problem. Here is my solution:

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    bool val;
    if (!bool.TryParse(value.ToString(), out val))
        return 0;

    int iParam;
    if (!int.TryParse(parameter.ToString(), out iParam))
        return 0;

    return val ? iParam : 0;
}
Community
  • 1
  • 1
RodMcKay
  • 37
  • 5
  • RaisePropertyChanged("myFilterRadioButtonInt"); is not changing the value of _Filter.FilterMyProperty. Post the converter. I bet the converter is changing the value. – paparazzo Nov 03 '14 at 13:41
  • @Blam added the converter to the question. – RodMcKay Nov 03 '14 at 14:21
  • normally only the getter is called, otherwise (if the setter was called) there would be some ***StackOverflow*** exception, there is obviously a call to `RaisePropertyChanged` in the setter. So I doubt there is still some other code triggering the setter, not the `RaisePropertyChanged()`. – King King Nov 03 '14 at 15:28

2 Answers2

1

I found this article which provides a consistent solution. I tried the solution in the OP and it didn't work consistently for me -- I had random instances where the selected radio button was not correctly reflected.

The ConvertBack should be:

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value.Equals(false) ? DependencyProperty.UnsetValue : parameter;
    }
AdvApp
  • 1,094
  • 1
  • 14
  • 27
0

Why are you returning the parameter as the value

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    return parameter;
}

The first button is going to set the value to true every time and the second to false every time.

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    Bool vBool = Bool.Parse(value);
    Int iParam = IntParse(parameter);
    if(iParm > 2 || iParem < 0) return false;
    if(vBool) return iParam;
    else if (iParam == 2) return 1;
    else return 2;
}

Also on the set if the current value is correct you should return immediately and not call RaisePropertyChanged.

paparazzo
  • 44,497
  • 23
  • 105
  • 176