DataBinding Radio Buttons

1.6k Views Asked by At

I will try to keep this concise as possible.

I am having an issue where I have a two way databinding with 3 radio buttons. My issue is, I am getting sort of a cyclical change as soon as I change the chosen radio button. So what is happening, I change the radio button which changes the source data property, but the source data property changes the other 2 properties that are bound, which in turn changes the other radio buttons, which calls the PropertyChange function on those properties as well. How can I fix it so that the PropertyChange only happens once per radio button switch.

Property Changed Event Handler:

public class SolutionOptions : INotifyPropertyChanged
{
    bool _direct;
    bool _iterative;
    bool _domain;
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    public bool Domain
    {
        get { return _domain; }
        set
        {
            if (_domain == value) return;

            _domain = value;
            OnPropertyChanged("Domain");
        }
    }

    public bool Iterative
    {
        get { return _iterative; }
        set
        {
            if (_iterative == value) return;

            _iterative = value;
            OnPropertyChanged("Iterative");
        }
    }

    public bool Direct
    {
        get { return _direct; }
        set
        {
            if (_direct == value) return;

            _direct = value;
            OnPropertyChanged("Direct");
        }
    }

Data Binding Code:

 this.radioButton_DirectSolver.DataBindings.Add("Checked", _ssf, "SolOptions.Direct");
 this.radioButton_IterSolver.DataBindings.Add("Checked", _ssf, "SolOptions.Iterative");
 this.radioButton_DomainDecomp.DataBindings.Add("Checked", _ssf, "SolOptions.Domain");

Form Image:

enter image description here

1

There are 1 best solutions below

3
On

First try adding this to all your setters

if (_domain == value) return;

So you don't call OnPropertyChanged if it does not change.
This is a good practice in general.

Second (only if the first does not work) put each button in a separate group so they don't call each other and handle setting others to false in your code behind.