WinForm binding radio button(WinForm 绑定单选按钮)
问题描述
我使用VS2010,然后将成员datagridview拖放到设计视图中.之后,我将名称成员文本字段拖放到设计视图,然后尝试编辑和保存.它工作正常.
I use VS2010 and then drag and drop Member datagridview to design view. After that I drag and drop name member textfield to design view and then try to edit and save. It's work properly.
然后我将性单选按钮拖放到设计视图中.但是绑定它不起作用.
And then I drag and drop sex radio button to design view. But binding it does't work.
在这种情况下我该如何绑定?
How can I binding in this situation?
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);
}
}
}
推荐答案
这里有两种可能的解决方案.
Here are two possible solutions.
绑定格式和解析事件
Binding
类有一个内置工具,用于以 格式 和 解析事件.
The Binding
class has a built-in facility for on-the-fly transformations of bound data in the form of the Format and Parse events.
以下是仅使用男性"单选按钮来使用这些事件的方法.在代码中创建绑定,而不是在设计器中:
Here's how you would use those events with just the "Male" radiobutton. Create the binding in code, not in the designer:
// 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;
<小时>
计算属性
另一种方法是向数据源添加计算属性:
Another approach is to add a computed property to your datasource:
public bool IsMale
{
get { return Sex == "M"; }
set
{
if (value)
Sex = "M";
else
Sex = "F";
}
}
现在您可以简单地将 Male 单选按钮绑定到数据源上的此属性(只是不要在网格中显示此属性).
Now you can simply bind the Male radiobutton to this property on your datasource (just don't show this property in the grid).
你也可以像这样将女性连接到男性:
And again you can hook up Female to Male like so:
maleRb.CheckedChanged += (s, args) => femaleRb.Checked = !maleRb.Checked;
这篇关于WinForm 绑定单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:WinForm 绑定单选按钮
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04