WinForm绑定单选按钮
我使用VS2010,然后将成员datagridview拖放到设计视图。 之后,我拖放名称成员文本框到设计视图,然后尝试编辑并保存。它工作正常。WinForm绑定单选按钮
然后我拖放性按钮设计视图。但绑定它不起作用。
如何在这种情况下绑定?
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
类有对即时绑定数据的转换在Format和Parse事件的形式内置设施。
下面是如何使用这些事件与“男性”单选按钮。在代码中创建绑定,而不是在设计师
// 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,如果我设置所有记录是女性,它将不会检查任何单选按钮。 [http://i.imgur.com/zo2KF.jpg] – 2012-01-19 04:51:47
解决方案#2,我可以在哪里添加代码? – 2012-01-19 05:07:02
我解决了绑定女按钮了。 – 2012-01-20 17:58:13
虽然我知道这已经回答了,我以为我会提供,让设计时结合能力的选项。
制作一个新的自定义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属性绑定到某个字符串属性即可。
限制:
- 两个在同一个单选按钮组不能有相同的文本(?但是这真的是一个限制)
- 在设计时,单选按钮的检查值可能看起来不准确(即同一组中的两个单选按钮可能会被检查)。但是,这不应该在运行时发生。
注意创建自定义的控制时,必须将项目的自定义对象是在工具箱中前建成。
制作表单时,您可以从“数据源”面板中拖动项目。在数据源面板中,您可以将类或表及其所有子项拖到窗体中,并自动生成文本框,或者在此场景中使用具有数据绑定的单选按钮。在数据库或类中,你必须为每个选项做一点点/布尔。
组单选按钮,添加一个单选按钮没有绑定,以保持所有的位/布尔变量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?副本,因此,这里是我的答案有一个副本。
memberBindingNavigatorSaveItem_Click和memberBindingNavigatorSaveItem_Click_1有什么区别?我错过了什么吗? +1,以便花时间发布代码和截图。 – CptSupermrkt 2012-01-18 06:58:36
这两个事件之间没有区别,当已经存在事件时,您创建了一个新的Click事件。 – 2012-01-18 07:00:09
是的,没有区别。 – 2012-01-18 07:02:02