Best way to databind a group of radiobuttons in WinForms

前端 未结 9 529
无人共我
无人共我 2020-12-03 00:58

I\'m currently working on databinding some of my existing Windows Forms, and I\'ve ran into an issue figuring out the proper way of databinding a group of radiobutton contro

相关标签:
9条回答
  • 2020-12-03 01:53

    I know this post is old but in my search for an answer for this same problem I came across this post and it didn't solve my problem. I ended up having a lightbulb go off randomly just a minute ago and wanted to share my solution.

    I have three radio buttons in a group box. I'm using a List<> of a custom class object as the data source.

    Class Object:

    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 ...
    

    Three seperate public variables with get/set accessing the same one global variable.

    In the code behind, I have a function called during the Page_Load() setting all the controls databindings. For each radio button I add its own databinging.

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

    I hope this helps someone!!

    0 讨论(0)
  • 2020-12-03 01:57

    Set the tag name of your radio buttons to something that represents the value.

    Create a string setting, for example, OptionDuplicateFiles, and give it the default value of the tag name for your default radio button.

    To save your checked radio button:

    Settings.Default.OptionDuplicateFiles = gbxDuplicateFiles.Controls
       .OfType<RadioButton>()
       .Where(b => b.Checked)
       .Select(b => b.Tag)
       .First()
       .ToString();
    

    To load your checked radio button:

    (gbxDuplicateFiles.Controls
       .OfType<RadioButton>()
       .Where(b => b.Tag.ToString() == Settings.Default.OptionDuplicateFiles)
       .First())
       .Checked = true;
    

    Tada!

    0 讨论(0)
  • 2020-12-03 01:58

    I think I would use my own GroupBox. I would bind the CustomGroupBox to your Model and set the correct RadioButton (using the tag or name properties) from the binded value.

    0 讨论(0)
提交回复
热议问题