I am trying to bind two boolean properties to two radio buttons. When the form loads, the two radio buttons are all unchecked. When I clicked the first radio button, it became checked. However, when I tried to click the second radio button, the first one stayed checked and the second one was still unchecked. If I clicked the second radio button again, both radio buttons are unchecked. If I clicked the second radio button again, it would become checked. Basically, it took 3 clicks to make the second radio button checked. Same applies to first radio button if I first checked the second radio button.
I have a viewmodel class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace WinFormsApp1
{
internal class ViewModel : INotifyPropertyChanged
{
private bool _isCashPayment;
private bool _isChequePayment;
public bool IsCashPayment
{
get => _isCashPayment;
set
{
if (_isCashPayment == value)
{
return;
}
_isCashPayment = value;
// Notify the UI that the property has changed.
OnPropertyChanged();
}
}
public bool IsChequePayment
{
get => _isChequePayment;
set
{
if (_isChequePayment == value)
{
return;
}
_isChequePayment = value;
// Notify the UI that the property has changed.
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler? PropertyChanged;
// Notify the UI that one of the properties has changed.
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
The form code:
namespace WinFormsApp1
{
public partial class Form7 : Form
{
private ViewModel _viewModel;
public Form7()
{
InitializeComponent();
_viewModel = new ViewModel();
}
private void Form7_Load(object sender, EventArgs e)
{
rdbCash.DataBindings.Add(nameof(rdbCash.Checked), _viewModel, nameof(_viewModel.IsCashPayment), false, DataSourceUpdateMode.OnPropertyChanged);
rdbCheque.DataBindings.Add(nameof(rdbCheque.Checked), _viewModel, nameof(_viewModel.IsChequePayment), false, DataSourceUpdateMode.OnPropertyChanged);
}
}
}
Form designer generated code:
namespace WinFormsApp1
{
partial class Form7
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
rdbCash = new RadioButton();
rdbCheque = new RadioButton();
button1 = new Button();
SuspendLayout();
//
// rdbCash
//
rdbCash.AutoSize = true;
rdbCash.Location = new Point(20, 66);
rdbCash.Name = "rdbCash";
rdbCash.Size = new Size(51, 19);
rdbCash.TabIndex = 0;
rdbCash.Text = "Cash";
rdbCash.UseVisualStyleBackColor = true;
//
// rdbCheque
//
rdbCheque.AutoSize = true;
rdbCheque.Location = new Point(136, 66);
rdbCheque.Name = "rdbCheque";
rdbCheque.Size = new Size(66, 19);
rdbCheque.TabIndex = 1;
rdbCheque.Text = "Cheque";
rdbCheque.UseVisualStyleBackColor = true;
//
// button1
//
button1.Location = new Point(15, 28);
button1.Name = "button1";
button1.Size = new Size(75, 23);
button1.TabIndex = 2;
button1.Text = "button1";
button1.UseVisualStyleBackColor = true;
//
// Form7
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(button1);
Controls.Add(rdbCheque);
Controls.Add(rdbCash);
Name = "Form7";
Text = "Form7";
Load += Form7_Load;
ResumeLayout(false);
PerformLayout();
}
#endregion
private RadioButton rdbCash;
private RadioButton rdbCheque;
private Button button1;
}
}
I am trying to use databinding instead of the traditional radio button checked event hander. I am expecting the radio button would change its state when clicked and also change the value of the binding property. I tried to put the two radio buttons in a group box and the result was the same.
user2836380 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.