WinForm绑定单选按钮 [英] WinForm binding radio button

查看:298
本文介绍了WinForm绑定单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VS2010,然后将成员datagridview拖放到设计视图。
之后,我将名称成员文本框拖放到设计视图,然后尝试编辑并保存。它正常工作



然后我拖放性感单选按钮进行设计视图。但是绑定它不起作用。



如何在这种情况下绑定?

 code> using System; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;

命名空间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:这行代码将数据加载到'dbDataSet会员表。您可以根据需要移动或移除它。
this.memberTableAdapter.Fill(this.dbDataSet.Member);
// TODO:这行代码将数据加载到'dbDataSet.Member'表中。您可以根据需要移动或移除它。
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);

}
}
}

解决方案

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






绑定格式和解析事件



绑定类有一个内置的功能,用于以绑定数据进行即时转换,形式为< a href =http://msdn.microsoft.com/en-us/library/system.windows.forms.binding.format.aspx =nofollow noreferrer>格式和解析事件。



这是如何使用这些事件与男单选按钮。在代码中创建绑定,而不是在设计器中创建绑定:

  //在Sex属性和RadioButton.Checked属性之间创建绑定b $ b var maleBinding = new Binding(Checked,bindingSource1,Sex); 
//当格式化(从数据源读取)时,为M返回true,否则为false
maleBinding.Format + =(s,args)=> args.Value =((string)args.Value)==M;
//当分析(写入数据源)时,返回M为true,否则为F
maleBinding.Parse + =(s,args)=> args.Value =(bool)args.Value? M:F;
//添加绑定
maleRb.DataBindings.Add(maleBinding);

//您不需要绑定女性单选按钮,只需通过处理Male上的CheckedChanged事件就可以做到对面的
//:
maleRb.CheckedChanged + =(s,args)=> femaleRb.Checked =!maleRb.Checked;






计算属性 / p>

另一种方法是将计算属性添加到数据源中:

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

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



再次,你可以连接女性到男性如下:

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


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 Format and Parse events

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;


Computed Property

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

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绑定单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆