【问题标题】:WinForm binding radio buttonWinForm 绑定单选按钮
【发布时间】:2012-02-12 22:42:43
【问题描述】:

我使用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 有什么区别?我错过了什么吗? +1 花时间发布代码和屏幕截图。
  • 这两个事件没有区别,当已有事件存在时,您创建了一个新的 Click 事件。
  • 您是否将单选按钮分组到 GroupBox 中,然后使用 CheckedChanged 事件?我认为这应该会给你足够的灵活性来完成这个任务。

标签: c# winforms data-binding datagridview radio-button


【解决方案1】:

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


绑定格式和解析事件

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

现在您可以简单地将 Male 单选按钮绑定到数据源上的此属性(只是不要在网格中显示此属性)。

你也可以像这样将女性连接到男性:

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

【讨论】:

  • 我尝试解决方案#1,如果我将所有记录设置为女性,则不会检查任何单选按钮。 [i.imgur.com/zo2KF.jpg]
  • 另一种解决方案只是确保男性单选按钮的初始状态为 Checked = true。这样当绑定发生时,它将被更改为 False,这将触发女性被更新。
  • 在Solution#2:有property这样的关键字吗?
  • @mmdemirbas:不,没有,感谢您指出这一点。有时我的手指直接与我的大脑相连,我的大脑用错误的语言思考。也许那是 Delphi 语法;多年前我是一名 Delphi 程序员。
【解决方案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. 在设计时,单选按钮的选中值可能看起来不准确(即,同一组中的两个单选按钮可能看起来已选中)。但是,这不应该在运行时发生。

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

【讨论】:

    【解决方案3】:

    制作表单时,您可以从“数据源”面板中拖动项目。在数据源面板中,您可以将一个类或表及其所有子项拖到表单中,并自动生成一个文本框,或者在这种情况下生成一个带有数据绑定的单选按钮。在数据库或类中,您必须为每个选项创建一个位 / bool。

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

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

    当保存更改时,循环遍历所有单选按钮,例如:

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

    我认为这是How do I use databinding with Windows Forms radio buttons? 的副本,因此,这是我的答案的副本。

    【讨论】:

      猜你喜欢
      • 2011-01-20
      • 2015-10-31
      • 2011-10-06
      • 1970-01-01
      • 2011-02-07
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      相关资源
      最近更新 更多