MVVM Bound Radio Button Unexpected Behavior
-
I have a boolean value to which radio buttons are bound, like so:
Yes No
In VM property looks like this:
private bool isChecked; public bool IsChecked { get { return isChecked; } set { if (isChecked!= value) { isChecked = value; OnPropertyChanged("IsChecked"); } } }
If the bound value is true, when I first open the screen the Yes checkbox is checked. I can select yes or no and they will display and save correctly. However, if I open the screen with a no value, nothing is checked. I believe the problem lies in the fact that when IsChecked is false, the converter converts that value to true in order to display the No box as checked, but in doing so it sets the IsChecked property to true, which then means that the No button should not be checked. The converter is the standard bool inverter all over the internet. I didnae write this code, just trying to debug it. Cheers, --EA
-
I have a boolean value to which radio buttons are bound, like so:
Yes No
In VM property looks like this:
private bool isChecked; public bool IsChecked { get { return isChecked; } set { if (isChecked!= value) { isChecked = value; OnPropertyChanged("IsChecked"); } } }
If the bound value is true, when I first open the screen the Yes checkbox is checked. I can select yes or no and they will display and save correctly. However, if I open the screen with a no value, nothing is checked. I believe the problem lies in the fact that when IsChecked is false, the converter converts that value to true in order to display the No box as checked, but in doing so it sets the IsChecked property to true, which then means that the No button should not be checked. The converter is the standard bool inverter all over the internet. I didnae write this code, just trying to debug it. Cheers, --EA
Have you tried setting
Mode=OneWay
on the "Nae" binding?<RadioButton x:Name="Nae"
GroupName="wah"
IsChecked="{Binding IsChecked, Converter={StaticResource boolInv}, Mode=OneWay}">
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer