简体   繁体   English

如何在VB.net和C#中绑定单选按钮?

[英]How to bind radio button in VB.net and C#?

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 解决方案在这里给出(请参阅): vb.net中的单选按钮和数据绑定

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." 问题是,我不知道如何遵循以下指示:“ ...然后,您可以将表单上的CarpetColor属性简单地绑定到数据源的CarpetColor。” Can you show me how to do it? 你能告诉我怎么做吗?

Also, for some reason, I cannot use the "INotifyPropertyChanged" interface (cannot be detected by intellisense). 另外,由于某种原因,我无法使用“ INotifyPropertyChanged”界面(智能感知无法检测到)。 Do I have to import any namespace? 我必须导入任何名称空间吗?

Edit: 编辑:

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

This is a portion of my code. 这是我的代码的一部分。 In my case, I am trying to make a simple student information with CRUD methods. 就我而言,我正在尝试使用CRUD方法制作一个简单的学生信息。 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 . 就我而言,我想绑定一个form属性

So after a long while, I finally figured out how to solve the problem. 因此,经过很长一段时间,我终于想出了解决问题的方法。

Actually, I have done the coding in VB.net since I am more familiar with the VB syntax instead of C#'s. 实际上,我已经在VB.net中完成了编码,因为我更熟悉VB语法而不是C#。 Nonetheless, the logic is pretty similar to each other since they are bounded by the .NET framework. 但是,由于它们之间受.NET框架的限制,因此逻辑彼此非常相似。

The form property goes as follows (though enumerations in the original post is also possible.): form属性如下(尽管原始帖子中的枚举也是可能的。):

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)

where Gender is the data member I need. Gender是我需要的数据成员。 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. 通过实现INotifyPropertyChanged接口解决了此问题。 I previously thought that this is implicitly included in every project, but easily found out that System.ComponentModel should be imported first. 我以前认为这隐式包含在每个项目中,但是很容易发现应该首先导入System.ComponentModel Thanks to MSDN . 感谢MSDN

Imports System.ComponentModel

Raising the protertyChanged event can be accomplished by: 可以通过以下方法来引发protertyChanged事件:

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

where RadioButton1 = radiobox for male and RadioButton2 = female (better to follow good naming conventions instead!). 其中RadioButton1 =男性的radiobox,RadioButton2 =女性(最好遵循良好的命名约定!)。

I am not that familiar with C# syntax but think that the equivalent code for raising checkChanged property is: 我不太熟悉C#语法,但是认为提高checkChanged属性的等效代码是:

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

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. 在VB6中,可以通过使用流行的记录集轻松实现这一点。 In .NET, the solution is not apparent. 在.NET中,解决方案并不明显。

在此处输入图片说明

Edit: I saw some other questions pertaining to the same scenario here in SO. 编辑:我在SO中看到了与同一场景有关的其他一些问题。 I found two, but they are both using C#, which may be fuzzy for VB users. 我发现了两个,但是它们都使用C#,这对于VB用户可能是模糊的。

Edit: 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. 这就是为什么我想更改此问题的标题以使其更加明确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM