WinForm绑定单选按钮

21

我使用VS2010,然后在设计视图中拖放成员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);

        }
    }
}

在此输入图片描述


memberBindingNavigatorSaveItem_Click 和 memberBindingNavigatorSaveItem_Click_1 有什么区别?我是不是漏掉了什么?感谢您花时间发布代码和截图。 - CptSupermrkt
这两个事件没有区别,当已经存在一个事件时,你创建了一个新的点击事件。 - Sai Kalyan Kumar Akshinthala
你是否将单选按钮分组在一个GroupBox中,然后使用CheckedChanged事件?这应该给你足够的灵活性来完成这个任务。 - CptSupermrkt
3个回答

19
这里有两种可能的解决方案。

绑定格式和解析事件

Binding类具有内置机制,可以在绑定数据时进行即时转换,这是通过 FormatParse 事件实现的。

以下是仅使用“男性”单选按钮的示例,创建绑定时应在代码中而非设计器中使用这些事件:

// 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";
    }
}

现在,您可以将男性单选按钮简单地绑定到数据源上的此属性(只需不在网格中显示此属性)。
然后,您可以像这样将女性连接到男性:
maleRb.CheckedChanged += (s, args) => femaleRb.Checked = !maleRb.Checked;

1
我尝试了解决方案#1,如果我将所有记录设置为女性,则不会选中任何单选按钮。[http://i.imgur.com/zo2KF.jpg] - korrawit
另一个解决方案就是确保男性单选按钮的初始状态为Checked = true。这样,在绑定发生时,它将被更改为False,并触发女性进行更新。 - Igby Largeman
在解决方案#2中:是否有关键字“property”? - mmdemirbas
@mmdemirbas:没有,谢谢你指出来。有时我的手指直接连接到我的大脑,而我的大脑正在用错误的语言思考。也许这就是Delphi语法;我很多年前是一名Delphi程序员。 - Igby Largeman

2

虽然我知道这个问题已经有答案了,但我想提供一种可以实现设计时绑定能力的选项。

创建一个新的自定义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. 在设计时,单选按钮的选中值可能不准确(即同一组中的两个单选按钮可能看起来已被选中)。然而,在运行时不应出现此问题。
注意:创建自定义控件时,必须在工具箱中构建项目,然后才能使用自定义对象。

0

在制作表单时,您可以从“数据源”面板中拖动项目。在数据源面板中,您可以选择一个类或表,它的所有子项都可以被拖到表单中,并将自动生成一个文本框或在这种情况下是一个带有数据绑定的单选按钮。在数据库或类中,您需要为每个选项创建一个位/布尔值。

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

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

在保存更改时,循环遍历所有单选按钮,如下所示:

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

我认为这是 如何在 Windows Forms 单选按钮中使用数据绑定? 的复制品。因此,这里是我在那里的回答的副本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接