在WinForms中数据绑定一组radiobutton的最佳方法

Gei*_*sve 25 c# data-binding radio-button winforms

我正在研究一些现有的Windows窗体的数据绑定,并且我遇到了一个问题,想出了在一个组框内数据绑定一组radiobutton控件的正确方法.

我的业务对象有一个整数属性,我想对4个radiobuttons进行数据绑定(其中每个都代表值0 - 3).

我目前正在绑定一个presenter对象,它作为表单和业务对象之间的绑定器,我现在的方式是拥有4个独立的属性,每个属性都绑定这些值(我使用INotifyPropertyChanged) ,但不包括这里):

Private int _propValue;

Public bool PropIsValue0 
{ 
  get { return _propValue == 0; }
  set
  {
    if (value) 
      _propValue = 0;
  }
}

Public bool PropIsValue1 { // As above, but with value == 1 }
Public bool PropIsValue2 { // As above, but with value == 2 }
Public bool PropIsValue3 { // As above, but with value == 3 }
Run Code Online (Sandbox Code Playgroud)

然后我将每个单选按钮绑定到它们各自的属性,如上所述.

这对我来说似乎不对,所以任何建议都受到高度赞赏.

Oha*_*der 23

下面是一个通用的RadioGroupBox实现,本着ArielBH的建议(一些代码借用了Jay Andrew Allen的RadioPanel).只需向其添加RadioButtons,将其标记设置为不同的整数并绑定到"Selected"属性.

public class RadioGroupBox : GroupBox
{
    public event EventHandler SelectedChanged = delegate { };

    int _selected;
    public int Selected
    {
        get
        {
            return _selected;
        }
        set
        {
            int val = 0;
            var radioButton = this.Controls.OfType<RadioButton>()
                .FirstOrDefault(radio =>
                    radio.Tag != null 
                   && int.TryParse(radio.Tag.ToString(), out val) && val == value);

            if (radioButton != null)
            {
                radioButton.Checked = true;
                _selected = val;
            }
        }
    }

    protected override void OnControlAdded(ControlEventArgs e)
    {
        base.OnControlAdded(e);

        var radioButton = e.Control as RadioButton;
        if (radioButton != null)
            radioButton.CheckedChanged += radioButton_CheckedChanged;
    }

    void radioButton_CheckedChanged(object sender, EventArgs e)
    {
        var radio = (RadioButton)sender;
        int val = 0;
        if (radio.Checked && radio.Tag != null 
             && int.TryParse(radio.Tag.ToString(), out val))
        {
            _selected = val;
            SelectedChanged(this, new EventArgs());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,由于InitializeComponent中的初始化顺序问题,您无法通过设计器绑定到"Selected"属性(绑定在初始化单选按钮之前执行,因此在第一次分配时它们的标记为null).所以就像这样绑定自己:

    public Form1()
    {
        InitializeComponent();
        //Assuming selected1 and selected2 are defined as integer application settings
        radioGroup1.DataBindings.Add("Selected", Properties.Settings.Default, "selected1");
        radioGroup2.DataBindings.Add("Selected", Properties.Settings.Default, "selected2");
    }
Run Code Online (Sandbox Code Playgroud)


小智 5

我知道这篇文章很旧,但在我寻找同样问题的答案时,我遇到了这篇文章,但它没有解决我的问题。就在一分钟前,我最终让一个灯泡随机熄灭,并想分享我的解决方案。

我在一个组框中有三个单选按钮。我使用自定义类对象的 List<> 作为数据源。

类对象:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BAL
{
class ProductItem
{

    // Global Variable to store the value of which radio button should be checked
    private int glbTaxStatus;
    // Public variable to set initial value passed from 
    // database query and get value to save to database
    public int TaxStatus
    {
        get { return glbTaxStatus; }
        set { glbTaxStatus = value; }
    }

    // Get/Set for 1st Radio button
    public bool Resale
    {
        // If the Global Variable = 1 return true, else return false
        get
        {
            if (glbTaxStatus.Equals(1))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        // If the value being passed in = 1 set the Global Variable = 1, else do nothing
        set
        {
            if (value.Equals(true))
            {
                glbTaxStatus = 1;
            }
        }
    }

    // Get/Set for 2nd Radio button
    public bool NeverTax
    {
        // If the Global Variable = 2 return true, else return false
        get
        {
            if (glbTaxStatus.Equals(2))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        // If the value being passed in = 2 set the Global Variable = 2, else do nothing
        set
        {
            if (value.Equals(true))
            {
                glbTaxStatus = 2;
            }
        }
    }

    // Get/Set for 3rd Radio button
    public bool AlwaysTax
    {
        // If the Global Variable = 3 return true, else return false
        get
        {
            if (glbTaxStatus.Equals(3))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        // If the value being passed in = 3 set the Global Variable = 3, else do nothing
        set
        {
            if (value.Equals(true))
            {
                glbTaxStatus = 3;
            }
        }
    }

// More code ...
Run Code Online (Sandbox Code Playgroud)

三个单独的公共变量,get/set 访问同一个全局变量。

在后面的代码中,我在 Page_Load() 设置所有控件数据绑定期间调用了一个函数。对于每个单选按钮,我添加了自己的数据绑定。

radResale.DataBindings.Add("Checked", glbProductList, "Resale", true, DataSourceUpdateMode.OnPropertyChanged, false);
radNeverTax.DataBindings.Add("Checked", glbProductList, "NeverTax", true, DataSourceUpdateMode.OnPropertyChanged, false);
radAlwaysTax.DataBindings.Add("Checked", glbProductList, "Always", true, DataSourceUpdateMode.OnPropertyChanged, false);
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助别人!!