Windows Forms databinding to radio buttons

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.

New contributor

user2836380 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật