1

I am using the MVVM pattern.

In my viewmodel I have a enum property for my radio button. This property is connected to my model's enum property:

public MyEnum MyEnumVar
{
     get { return MyModel.EnumVar; }
     set { MyModel.EnumVar = value; }
}

If I change to a different view and then change back to the original view, instead of the getter being called the setter gets called, and the value passed is the first option for my radio buttons.

Basically every time i switch views my models radio button's enum gets reset. Is there a way to stop the setter from being called?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Bob
  • 821
  • 1
  • 17
  • 36
  • How do you bind your property? – H.B. Jul 27 '12 at 20:50
  • @H.B. IsChecked="{Binding Path=MyEnumVar, Converter={StaticResource myEnumBooleanConverter}, ConverterParameter=MyEnumVal1, Mode=TwoWay}" – Bob Jul 27 '12 at 20:52
  • @H.B. I am using a converter so that I can store the resulting boolean into my enum property. – Bob Jul 27 '12 at 20:53
  • @Chris: Drop a big old fat breakpoint in your converter methods and watch what it does when you switch back. Bet you have some issues there. –  Jul 27 '12 at 21:03
  • The converter seems to work just fine. I can select different radio buttons but if for example I select option #2 and I switch views and come back to the original view #2 will no longer be selected, #1 will be. – Bob Jul 27 '12 at 21:25

2 Answers2

0

I am using a converter so that I can store the resulting boolean into my enum property.

Converters are a horrible solution which by now is widespread and the accepted answer to quite a few duplicate questions on binding RadioButtons, the "proper" way is using a ListBox and the SelectedItem property, that way not a multitude of RadioButtons can fight over your property.

An example of how to do this can be seen in this answer. If you switch view the SelectedItem should bind source to target, selecting the RadioButton with the respective value.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
0

I used the solution from this tutorial: http://wpftutorial.net/RadioButton.html

Then my setters were called from the UI and changed all the active radio button states. For me it helped then to set a new groupname for each radio button, so that they are not in the same group and not influencing each other.

<RadioButton Grid.Row="1" Margin="20,30,0,40" GroupName="{Binding GroupName}" Content="Yes" IsChecked="{Binding RadioButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource EnumMatchToBooleanConverter}, ConverterParameter=Yes}" />
<RadioButton Grid.Row="1" Margin="76,30,0,40" GroupName="{Binding GroupName}" Content="No" IsChecked="{Binding RadioButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource EnumMatchToBooleanConverter}, ConverterParameter=No}"/>

And in the ViewModel:

public string GroupName
{
    get
    {
        return Guid.NewGuid().ToString();
    }
}
SeBo
  • 275
  • 1
  • 3
  • 10