WinForm 绑定单选按钮

     2023-02-23     120

关键词:

【中文标题】WinForm 绑定单选按钮【英文标题】:WinForm binding radio button 【发布时间】: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 事件?我认为这应该会给你足够的灵活性来完成这个任务。 【参考方案1】:

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


绑定格式和解析事件

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

现在您可以简单地将 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 属性绑定到某个字符串属性。

限制:

    同一组中的两个单选按钮不能有相同的文本(但这真的有限制吗?) 在设计时,单选按钮的选中值可能看起来不准确(即,同一组中的两个单选按钮可能看起来已选中)。但是,这不应该在运行时发生。

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

【讨论】:

【参考方案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? 的副本,因此,这是我的答案的副本。

【讨论】:

单选按钮、绑定、转换器

】单选按钮、绑定、转换器【英文标题】:RadioButton,Binding,Converters【发布时间】:2014-07-1211:53:32【问题描述】:我正在使用VisualStudio2008,但我遇到了单选按钮问题。我有3个单选按钮:<Window.Resources>//[...]<DataTemplatex:Key="grid... 查看详情

绑定变量 PyQt 单选按钮

】绑定变量PyQt单选按钮【英文标题】:BindingVariablePyQtRadioButtons【发布时间】:2014-06-2118:38:58【问题描述】:我正在使用PyQt4并希望设置一个单选按钮列表,这些单选按钮设置一个取决于选择的变量值。显然我可以用大量的IF语句... 查看详情

简单的 WPF 单选按钮绑定?

】简单的WPF单选按钮绑定?【英文标题】:SimpleWPFRadioButtonBinding?【发布时间】:2010-11-2200:27:32【问题描述】:将一组3个单选按钮绑定到值1、2或3的int类型属性的最简单方法是什么?【问题讨论】:看看这篇博文:>WPFRadioButtons... 查看详情

带单选按钮的角度动态 html 绑定

】带单选按钮的角度动态html绑定【英文标题】:angularDynamichtmlbindingwithradiobuttons【发布时间】:2019-12-1602:31:01【问题描述】:我从API获取HTML数据,这个Html有两个单选按钮和一些文本。在我的应用程序中,我通过[innerHtml]="example|s... 查看详情

mvc 绑定/发布布尔到单选按钮

】mvc绑定/发布布尔到单选按钮【英文标题】:mvcbind/postbooleantoradiobutton【发布时间】:2011-02-1613:23:29【问题描述】:我的模型中有一个带有NULLABLE布尔值的列。现在在我的视图(用于编辑)上,我想将其绑定到两个单选按钮:是... 查看详情

如何在单选按钮上绑定数据

】如何在单选按钮上绑定数据【英文标题】:Howtobinddataonradiobutton【发布时间】:2014-06-1308:01:04【问题描述】:我有一个带有一些单选按钮的xml视图,必须将数据绑定到模型。我正在使用openui5-runtime-1.22.4。我有以下控制器,它添... 查看详情

不使用 NSMatrix 的 Cocoa 单选按钮可以绑定吗?

】不使用NSMatrix的Cocoa单选按钮可以绑定吗?【英文标题】:CanCocoaradiobuttonsthatdonotuseNSMatrixhavebindings?【发布时间】:2016-01-1805:32:29【问题描述】:在之前的项目中,我使用了一个NSMatrix对象来实现三个单选按钮。我没有与这些按... 查看详情

使用 JFace 数据绑定绑定单选按钮组的正确方法

】使用JFace数据绑定绑定单选按钮组的正确方法【英文标题】:ProperwaytobindaradiobuttongroupusingJFacedatabinding【发布时间】:2010-10-1315:38:19【问题描述】:我想知道是否有人可以向我解释如何使用JFace数据绑定将一组单选按钮正确绑定... 查看详情

Angular2 - 单选按钮绑定

】Angular2-单选按钮绑定【英文标题】:Angular2-RadioButtonBinding【发布时间】:2015-10-3100:04:03【问题描述】:我想在使用Angular2的表单中使用单选按钮Options:<br/>1:<inputname="options"ng-control="options"type="radio"value="1"[(ng-model)]="model.opti... 查看详情

Winforms 将枚举绑定到单选按钮

】Winforms将枚举绑定到单选按钮【英文标题】:WinformsBindEnumtoRadioButtons【发布时间】:2011-01-2910:17:16【问题描述】:如果我有三个单选按钮,将它们绑定到具有相同选择的枚举的最佳方法是什么?例如[]Choice1[]Choice2[]Choice3publicenum... 查看详情

将组 2 中的一个单选按钮的内容与组 1 中选定的单选按钮绑定

】将组2中的一个单选按钮的内容与组1中选定的单选按钮绑定【英文标题】:BindingcontentofoneRadiobuttoningroup2withselectedradiobuttoningroup1【发布时间】:2014-06-2612:23:52【问题描述】:我的WPFxaml中有两组单选按钮。<StackPanelMargin="5,-4,0,5... 查看详情

在 WinForms 中对一组单选按钮进行数据绑定的最佳方法

】在WinForms中对一组单选按钮进行数据绑定的最佳方法【英文标题】:BestwaytodatabindagroupofradiobuttonsinWinForms【发布时间】:2010-10-1501:53:27【问题描述】:我目前正在对我现有的一些Windows窗体进行数据绑定,但我遇到了一个问题,... 查看详情

MVVM:将单选按钮绑定到视图模型?

】MVVM:将单选按钮绑定到视图模型?【英文标题】:MVVM:Bindingradiobuttonstoaviewmodel?【发布时间】:2011-01-1802:10:43【问题描述】:编辑:问题已在.NET4.0中修复。我一直在尝试使用IsChecked按钮将一组单选按钮绑定到视图模型。在查看... 查看详情

将单选按钮绑定到复杂对象可防止选中

】将单选按钮绑定到复杂对象可防止选中【英文标题】:Bindingradiobuttontocomplexobjectpreventsselected【发布时间】:2016-05-2122:37:15【问题描述】:我正在将我的单选按钮绑定到我的模型,如下所示:<inputtype="radio"ng-model="formData.style"n... 查看详情

为啥视图模型中的单选按钮绑定被触发不止一次

】为啥视图模型中的单选按钮绑定被触发不止一次【英文标题】:Whyradiobuttonbindinginviewmodelgetsfiredmorethanonetime为什么视图模型中的单选按钮绑定被触发不止一次【发布时间】:2018-10-1312:49:06【问题描述】:我在这里有我的windowspho... 查看详情

将真/假绑定到 Knockout JS 中的单选按钮

】将真/假绑定到KnockoutJS中的单选按钮【英文标题】:Bindingtrue/falsetoradiobuttonsinKnockoutJS【发布时间】:2012-04-2500:01:53【问题描述】:在我的视图模型中,我有一个值为真或假的IsMale值。在我的UI中,我希望将其绑定到以下单选按... 查看详情

绑定敲除数组以呈现 jqm 单选按钮

】绑定敲除数组以呈现jqm单选按钮【英文标题】:Bindingknockoutarraytorenderjqmradiobuttons【发布时间】:2014-09-2405:22:01【问题描述】:我正在尝试呈现单选按钮列表。我从这个url修改了代码示例,最后得到了这段废话/代码,http://demos.... 查看详情

单选按钮的 knockout.js 布尔数据绑定问题

】单选按钮的knockout.js布尔数据绑定问题【英文标题】:knockout.jsbooleandata-bindingissueswithradiobuttons【发布时间】:2012-06-1322:09:47【问题描述】:我目前正在执行以下操作以补偿布尔值不能很好地映射到单选按钮。由于字段是如何从... 查看详情