Reputation: 619
I have several RadioButtons combined in one Group so that only one can be checked. The RadioButtons are bound to boolean Properties.
This works very well and my program reacts on the changes of these Properties.
Suppose optA
is selected. Next I select optB
, the value of PropertyB
is set to true and my program reacts accordingly. However, at this time both optA
and optB
are true until the Group of Radio buttons updates optA
to false which again triggers my "IsSelected" event.
I think this is totally correct behavior... But how can I distinguish between a change from the user and the automated change from the group?
Edit: Code
<RadioButton Name="featureCheckBox" GroupName="featureRadioButtonGroup" IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" />
public bool IsSelected {
get{ return _isSelected; }
set { PropertyChanged.ChangeAndNotify(ref _isSelected, value, () => IsSelected); }
}
Upvotes: 0
Views: 3894
Reputation: 1499
First of all to answer your question.
Change the set portion of the IsSelected property as follows. (You will of course have to write the actual code for the if statement)
public bool IsSelected
{
get{ return _isSelected; }
set
{
//If two options are true wait for the second change of values
//You will now only get One notification per change.
if (twoOptions are selected) return;
else PropertyChanged.ChangeAndNotify(ref _isSelected, value, () => IsSelected);
}
}
You could instead just listen for false values but then you'd have to set an arbitrary options to true to begin with. Or you could just listen for true values and ignore all false values which means you will have problems with multiple Properties being true until the group box updates the old option to false.
Now for the advice.
A much better model would be this.
but do NOT group them
. (Grouping them causes the UI to force the selection of only one. This does not lend itself to separation of UI and business logic.)Several benefits of doing it this way.
ANY
code.)Upvotes: 2
Reputation: 5715
I think you can distinguish it by its value. True
is always set from user because you cannot unselected the radio butoon and false
is always comes from another value set to true
.
Upvotes: 0