RaisePropertyChanged sets the value of the switches

I have strange behavior if I want to update my RaisePropertyChanged UI. I am using the second solution (from Johnathan1) of this post : I have implemented RadioBoolToIntConverter.

My virtual machine 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");
            }
        }

      

The converter looks like this (by Jonathan1 from 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

, this is the bool value of the model, which is responsible for the fact that my value, which will be filtered, is shown or not shown. This is due to 2 radio remotes using 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 RadioButtons works correctly.

The problem is that I am setting _Filter.FilterMyProperty = true

by code (setting the standard filter where this value should be filtered) and then the RaisePropertyChanged("myFilterRadioButtonInt")

value _Filter.FilterMyProperty

will be set to false

.

Edit:

By RaisePropertyChanged("myFilterRadioButtonInt")

(called by the filter property setter in the VM) the setter myFilterRadioButtonInt

is called again and it will set the current value of the RadioBox (in my case value

there is 2

so the setter will return the value _Filter.FilterMyProperty

to false

.

It is not possible to change the RadioBoxes value by code using this method. I thought RaisePropertyChanged("myFilterRadioButtonInt")

only the receiver would be called when called . How can I solve this problem and why is the caller being called RaisePropertyChanged("myFilterRadioButtonInt")

?

Edit:

There was a problem returning a parameter to ConvertBack (). 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;
}

      

+3


source to share


2 answers


Why are you returning parameter as value

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

      

The first button will set 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, if set to the current value, you should return immediately and not call RaisePropertyChanged.

0


source


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 incorrectly reflected.

ConvertBack should be:



    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value.Equals(false) ? DependencyProperty.UnsetValue : parameter;
    }

      

+1


source







All Articles