dotnet/winforms

Multiple RadioButton in GroupBox do not DataBind to checked correctly

开放

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

 (3 条评论) (1 个反应) (0 位负责人)C# (922 个派生)batch import
:beetle: bughelp wanted

仓库指标

星标
 (4,100 个星标)
PR 合并指标
 (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

贡献者指南