如何在VB.net和C#中绑定单选按钮? [英] How to bind radio button in VB.net and C#?

查看:102
本文介绍了如何在VB.net和C#中绑定单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将表单属性绑定到数据源时遇到问题。解决方案在此处提供(请参阅):vb.net中的单选按钮和数据绑定

I have a problem about binding a form property to a datasource. The solution is given here(Please see): Radio Buttons And Databinding in vb.net

问题是,我不知道如何遵循指令- ...然后您可以将表单上的CarpetColor属性绑定到数据源的CarpetColor。

The problem is, I do not know how to follow the instruction- "...and then you can simply bind the CarpetColor property on your form to the data source's CarpetColor." Can you show me how to do it?

此外,由于某些原因,我无法使用 INotifyPropertyChanged界面(intellisense无法检测到)。我必须导入任何名称空间吗?

Also, for some reason, I cannot use the "INotifyPropertyChanged" interface (cannot be detected by intellisense). Do I have to import any namespace?

编辑:

enum Gender { Male,Female }
        public Gender _Gender
        {
            get { 
                if(radMale.Checked == true)
                    return Gender.Male;
               else
                    return Gender.Female;
            }
            set
            {
                if (value == Gender.Male)
                    radMale.Checked = true;
                else
                    radFemale.Checked = true;
            }
        }

这是我代码的一部分。就我而言,我正在尝试使用CRUD方法制作一个简单的学生信息。我对如何绑定单选按钮感到困惑,但发现可以使用表单的属性来实现它。绑定到任何其他控件(如文本框和组合框)非常容易。就我而言,我想绑定 form属性

This is a portion of my code. In my case, I am trying to make a simple student information with CRUD methods. I am stucked on how to bind a radio button but I found out that I can implement it using a form's property instead. Binding to any other control such as textboxes and comboboxes is fairly easy. In my case, I want to bind a form property.

推荐答案

因此,很长一段时间后,我终于想出了解决问题的方法。

So after a long while, I finally figured out how to solve the problem.

实际上,我已经在VB.net中完成了编码,因为我更熟悉VB语法而不是C#。但是,由于彼此之间受.NET框架的约束,因此逻辑彼此非常相似。

Actually, I have done the coding in VB.net since I am more familiar with the VB syntax instead of C#'s. Nonetheless, the logic is pretty similar to each other since they are bounded by the .NET framework.

form属性如下(尽管原始文章中的枚举也

The form property goes as follows (though enumerations in the original post is also possible.):

Public Property propGender() As String
    Get
        If RadioButton1.Checked = True Then
            Return "M"
        Else
            Return "F"
        End If
    End Get
    Set(ByVal value As String)
        _Sex = value
        If _Sex = "M" Then
            RadioButton1.Checked = True
        Else
            RadioButton2.Checked = True
        End If
    End Set
End Property

这很简单,但我仅包含该代码段以作进一步说明。

That is trivial, but I only included that code snippet for further clarifications.

关于如何对表单属性进行数据绑定的答案很简单:

The answer on how to data-bind a form property is simply:

Dim genderBinder = New Binding("propGender", Tbl_Stud_InfoBindingSource, "Gender")
Me.DataBindings.Add(genderBinder)
Me.DataBindings.Add(genderBinder)

其中,性别是我需要的数据成员。使用单选按钮调整表单属性会让我感到困惑(坦白地说,直到现在)。通过实现 INotifyPropertyChanged 接口解决了此问题。我以前认为这隐式包含在每个项目中,但是很容易发现 System.ComponentModel 应该首先导入。感谢 MSDN

where Gender is the data member I need. Adjusting the form properties with the use of the radio buttons is where I get confused(even until now, honestly). This problem was solved by implementing the INotifyPropertyChanged interface. I previously thought that this is implicitly included in every project, but easily found out that System.ComponentModel should be imported first. Thanks to MSDN.

Imports System.ComponentModel

引发protertyChanged事件可以通过以下方式完成:

Raising the protertyChanged event can be accomplished by:

Protected Sub OnPropertyChanged(ByVal name As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
    OnPropertyChanged("propGender")
End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
    OnPropertyChanged("propGender")
End Sub

其中,RadioButton1 =男性的Radiobox,RadioButton2 =女性(更好跟随

where RadioButton1 = radiobox for male and RadioButton2 = female (better to follow good naming conventions instead!).

我不太熟悉C#语法,但认为提高checkChanged属性的等效代码是:

I am not that familiar with C# syntax but think that the equivalent code for raising checkChanged property is:

RadioButton1.CheckedChanged += (s, args) => OnPropertyChanged("propGender");
RadioButton2.CheckedChanged += (s, args) => OnPropertyChanged("propGender");

最后,通过这种方式,我现在可以轻松地绑定单选按钮-我知道可以容易解决,但像我这样的初学者经常遇到的问题。在VB6中,可以使用流行的记录集轻松实现这一点。在.NET中,解决方案并不明显。

Finally, in this way, I can now easily manage to bind radio buttons - I know it can be easy to solve this but a common question by beginners like me. In VB6, this can be implemented easily with the use of the popular recordsets. In .NET, the solution is not apparent.

编辑:
我在SO中还看到了与同一场景有关的其他一些问题。我发现了两个,但是它们都使用C#,这对VB用户而言可能是模糊的。

I saw some other questions pertaining to the same scenario here in SO. I found two, but they are both using C#, which may be fuzzy for VB users.

编辑:
我曾经弄清楚的术语只是数据绑定单选按钮。这就是为什么我想更改此问题的标题以使其更加明确。

The missing key terminology I used to figure out is simply "Data binding radio button". That's why I want to change the title of this question to make it more definitive.

这篇关于如何在VB.net和C#中绑定单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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