Best way to databind a group of radiobuttons in WinForms

前端 未结 9 567
无人共我
无人共我 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:36

    My approach is to put each radio button into its own panel before binding them to a boolean property:

        public static Binding Bind(this RadioButton control, object dataSource, string dataMember)
        {
            // Put the radio button into its own panel
            Panel panel = new Panel();
            control.Parent.Controls.Add(panel);
            panel.Location = control.Location;
            panel.Size = control.Size;
            panel.Controls.Add(control);
            control.Location = new Point(0, 0);
    
            // Do the actual data binding
            return control.DataBindings.Add("Checked", dataSource, dataMember);
        }
    

提交回复
热议问题