WinForm绑定单选按钮

我使用VS2010,然后将Member datagridview拖放到设计视图中。 之后,我将名称成员文本字段拖放到设计视图,然后尝试编辑和保存。 它运作正常。

然后我拖放性爱单选按钮来设计视图。 但绑定它不起作用。

我怎么能在这种情况下绑定?

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Test7 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void memberBindingNavigatorSaveItem_Click(object sender, EventArgs e) { this.Validate(); this.memberBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll(this.dbDataSet); } private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'dbDataSet.Member' table. You can move, or remove it, as needed. this.memberTableAdapter.Fill(this.dbDataSet.Member); // TODO: This line of code loads data into the 'dbDataSet.Member' table. You can move, or remove it, as needed. this.memberTableAdapter.Fill(this.dbDataSet.Member); } private void memberBindingNavigatorSaveItem_Click_1(object sender, EventArgs e) { this.Validate(); this.memberBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll(this.dbDataSet); } } } 

在此处输入图像描述

这是两种可能的解决方案。


绑定格式和解析事件

Binding类具有内置function,可以以Format和Parse事件的forms对绑定数据进行动态转换。

以下是如何使用“男性”单选按钮进行这些事件。 在代码中创建绑定,而不是在设计器中:

 // create binding between "Sex" property and RadioButton.Checked property var maleBinding = new Binding("Checked", bindingSource1, "Sex"); // when Formatting (reading from datasource), return true for M, else false maleBinding.Format += (s, args) => args.Value = ((string)args.Value) == "M"; // when Parsing (writing to datasource), return "M" for true, else "F" maleBinding.Parse += (s, args) => args.Value = (bool)args.Value ? "M" : "F"; // add the binding maleRb.DataBindings.Add(maleBinding); // you don't need to bind the Female radiobutton, just make it do the opposite // of Male by handling the CheckedChanged event on Male: maleRb.CheckedChanged += (s, args) => femaleRb.Checked = !maleRb.Checked; 

计算财产

另一种方法是向您的数据源添加计算属性:

 public bool IsMale { get { return Sex == "M"; } set { if (value) Sex = "M"; else Sex = "F"; } } 

现在,您只需将Male radiobutton绑定到数据源上的此属性(只是不要在网格中显示此属性)。

你可以再次将女性与男性联系起来:

 maleRb.CheckedChanged += (s, args) => femaleRb.Checked = !maleRb.Checked; 

虽然我意识到这已经得到了回答,但我想我会提供一个提供设计时绑定能力的选项。

制作一个新的Custom RadioButton对象。 这是通过以下代码完成的。

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace MSLabExample { class RadioButtonBind : RadioButton { private string _selectValue = string.Empty; public string SelectValue { get { return _selectValue; } set { if (value == Text) Checked = true; else Checked = false; _selectValue = value; } } public RadioButtonBind() { this.CheckedChanged += new EventHandler(RadioButtonBind_CheckedChanged); this.TextChanged += new EventHandler(RadioButtonBind_TextChanged); } void RadioButtonBind_TextChanged(object sender, EventArgs e) { if (Checked) _selectValue = Text; } void RadioButtonBind_CheckedChanged(object sender, EventArgs e) { if (Checked) _selectValue = Text; } } } 

上述控件的基本概念是使用可绑定的字符串值,并在绑定的字符串值等于单选按钮文本时自行检查。

通过使用单选按钮文本,可以轻松了解正确的选中值,还可以通过更改单选按钮文本来更新SelectValue。 修改单选按钮时不需要额外的代码。

现在,您只需将同一组中所有单选按钮的SelectedValue属性绑定到某个字符串属性即可。

限制:

  1. 同一组中的两个单选按钮不能具有相同的文本(但这真的是一个限制吗?)
  2. 在设计时,单选按钮的检查值可能看起来不准确(即,可能看起来检查同一组中的两个单选按钮)。 但这不应该在运行时发生。

请注意,在创建自定义控件时,必须在工具箱中的自定义对象可用之前构建项目。

制作表单时,您可以从“数据源”面板中拖动项目。 在数据源面板中,您可以将类或表及其所有子项拖动到表单中,并自动生成文本框,或者在此方案中使用带有数据绑定的单选按钮。 在数据库或类中,您必须为每个选项做一点/ bool。

将单选按钮分组并添加一个没有数据绑定的单选按钮以保持所有位/ bool 0。

将所有单选按钮的CausesValidation设置为False。

通过所有单选按钮保存更改循环时,例如:

 ((YourClass)myBindingSource.DataSource).property1 = radioButton1.Checked; ((YourClass)myBindingSource.DataSource).property2 = radioButton2.Checked; 

我认为这是如何使用Windows窗体单选按钮进行数据绑定的副本? 因此,这里是我的答案的副本。