dotnet/winforms

Multiple RadioButton in GroupBox do not DataBind to checked correctly

Open

#5,072 创建于 2021年6月10日

在 GitHub 查看
 (3 评论) (1 反应) (0 负责人)C# (922 fork)batch import
:beetle: bughelp wanted

仓库指标

Star
 (4,100 star)
PR 合并指标
 (平均合并 14天 22小时) (30 天内合并 56 个 PR)

描述

.NET Core Version:

SDK: 5.0.203 (383637d63f) Runtime: 5.0.6 (478b2f8c0e)

Problem description:

When a property is bound to the checked property of a RadioButton and that RadioButton is in a group with other RadioButtons its checked property does not propagate back to the bound property when another RadioButton in the group is checked.

Minimal repro:

  • Create new .NET-Core 5.0 WinForms app
  • Open the forms designer for Form1
  • Add GroupBox
  • Add 3 RadioButtons to the GroupBox
  • Add a Button
  • Replace Form1.cs with the following:
using System;
using System.Windows.Forms;

namespace RadioButtonDataBindingTest
{
  public partial class Form1 : Form
  {
    private readonly RadioSelectionGroup radioSelectionGroup = new RadioSelectionGroup()
    {
      RadioButton1Selected = false,
      RadioButton2Selected = false,
      RadioButton3Selected = true
    };

    public Form1()
    {
      this.InitializeComponent();

      this.radioButton1.DataBindings.Add(nameof(this.radioButton1.Checked), radioSelectionGroup, nameof(radioSelectionGroup.RadioButton1Selected));
      this.radioButton2.DataBindings.Add(nameof(this.radioButton2.Checked), radioSelectionGroup, nameof(radioSelectionGroup.RadioButton2Selected));
      this.radioButton3.DataBindings.Add(nameof(this.radioButton3.Checked), radioSelectionGroup, nameof(radioSelectionGroup.RadioButton3Selected));
    }

    private void button1_Click(object sender, EventArgs e)
    {
      string message1 = $"radioButton1.Checked = {this.radioButton1.Checked} <=> RadioButton1Selected = {this.radioSelectionGroup.RadioButton1Selected}";
      string message2 = $"radioButton2.Checked = {this.radioButton2.Checked} <=> RadioButton2Selected = {this.radioSelectionGroup.RadioButton2Selected}";
      string message3 = $"radioButton3.Checked = {this.radioButton3.Checked} <=> RadioButton3Selected = {this.radioSelectionGroup.RadioButton3Selected}";

      MessageBox.Show(message1 + Environment.NewLine + message2 + Environment.NewLine + message3);
    }

    private class RadioSelectionGroup
    {
      public bool RadioButton1Selected { get; set; }
      public bool RadioButton2Selected { get; set; }
      public bool RadioButton3Selected { get; set; }
    }
  }
}

The sample starts with RadioButton#3 as selected, now when e.g. RadioButton#2 gets selected, one would expect that RadioButton2Selected of the RadioSelectionGroup becomes true AND RadioButton3Selected becomes false.

The screenshot below shows that actually RadioButton3Selected stays true.

image

The complete sample is attached here: RadioButtonDataBindingTest.zip

贡献者指南