系统相关
首页 > 系统相关> > c#-Windows窗体RadioButton列表-将Enum属性绑定到RadioButton

c#-Windows窗体RadioButton列表-将Enum属性绑定到RadioButton

作者:互联网

假设我只有几个单选按钮和一些自定义对象作为数据源.

举个例子

public enum SomeModeType
{
    firstMode = 10,
    secondMode = 20,
    thirdMode = 30
}

public class MyCustomObject:INotifyPropertyChanged
{

    private SomeModeType _mode;
    public SomeModeType Mode
    {
        set { _mode = value; }
        get { return _mode; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

如何使用以下方式将此对象属性(如果可能)绑定到3个不同的单选按钮:

如果单选按钮被选中-对象属性模式设置为firstMode

如果选中单选按钮-对象属性模式设置为secondMode

如果单选按钮三被选中-对象属性模式设置为thirdMode

还是更好地为此使用事件?

附言
我知道如何使用事件,但是它不知所措,无法按事件创建事件,例如rb1chnaged,rb2changed,…,rb100changed,不是吗?

P.P.S.

圣诞快乐!

解决方法:

对于枚举的每个值,您需要创建一个RadioButton并将其Checked值绑定到数据源的Mode属性.然后,您需要使用Binding的Format和Parse事件将Mode值转换为Checked属性的合适值,反之亦然.

示例-使用FlowLayoutPanel的单选按钮列表

例如,将FlowLayoutPanel控件放在表单上,​​然后在Form的Load事件中编写以下代码.该代码将动态地将RadioButton控件添加到流布局面板中并执行数据绑定:

var enumValues = Enum.GetValues(typeof(SomeModeType)).Cast<object>()
    .Select(x => new { Value = x, Name = x.ToString() }).ToList();
enumValues.ForEach(x =>
{
    var radio = new RadioButton() { Text = x.Name, Tag = x.Value };
    var binding = radio.DataBindings.Add("Checked", dataSource,
        "Mode", true, DataSourceUpdateMode.OnPropertyChanged);
    binding.Format += (obj, ea) =>
    { ea.Value = ((Binding)obj).Control.Tag.Equals(ea.Value); };
    binding.Parse += (obj, ea) =>
    { if ((bool)ea.Value == true) ea.Value = ((Binding)obj).Control.Tag; };
    flowLayoutPanel1.Controls.Add(radio);
});

在以上示例中,数据源可以是MyCustomObject或BindingList< MyCustomObject>.或包含List< MyCustomObject>的BindingSource.在其数据源中.

另一种选择-使用Owner-draw ListBox的RadioButton List

作为另一个选择,您可以使用所有者绘制的ListBox并为项目渲染RadioButton.这样,您可以将ListBox的SelectedValue绑定到对象的Mode属性.以下代码中的dataSourcs可以类似于上面的示例.将一个列表框放在窗体上,并在窗体的Load事件中编写以下代码:

var enumValues = Enum.GetValues(typeof(SomeModeType)).Cast<object>()
    .Select(x => new { Value = x, Name = x.ToString() }).ToList();
this.listBox1.DataSource = enumValues;
this.listBox1.ValueMember = "Value";
this.listBox1.DisplayMember = "Name";
this.listBox1.DataBindings.Add("SelectedValue", dataSource,
        "Mode", true, DataSourceUpdateMode.OnPropertyChanged);
this.listBox1.DrawMode = DrawMode.OwnerDrawFixed;
this.listBox1.ItemHeight = RadioButtonRenderer.GetGlyphSize(
    Graphics.FromHwnd(IntPtr.Zero),
            RadioButtonState.CheckedNormal).Height + 4;
this.listBox1.DrawItem += (obj, ea) =>
{
    var lb = (ListBox)obj;
    ea.DrawBackground();
    var text = lb.GetItemText(lb.Items[ea.Index]);
    var r = ea.Bounds;
    r.Offset(ea.Bounds.Height, 0);
    RadioButtonRenderer.DrawRadioButton(ea.Graphics, 
        new Point(ea.Bounds.Location.X, ea.Bounds.Location.Y + 2), r, text,
        lb.Font, TextFormatFlags.Left, false,
        (ea.State & DrawItemState.Selected) == DrawItemState.Selected ?
        RadioButtonState.CheckedNormal : RadioButtonState.UncheckedNormal);
};

屏幕截图

您可以在下图中看到两个解决方案:

enter image description here

var list = new List<MyCustomObject>() { 
    new MyCustomObject(){ Mode= SomeModeType.firstMode},
    new MyCustomObject(){ Mode= SomeModeType.secondMode},
    new MyCustomObject(){ Mode= SomeModeType.thirdMode},
};
this.myCustomObjectBindingSource.DataSource = list;
var dataSource = myCustomObjectBindingSource;

注意

回答此问题后,我在以下文章中创建并共享了RadioButtonList控件:WinForms RadioButtonList doesn’t exist.

它具有数据绑定支持,您可以像ListBox一样使用此控件.为此,将其绑定到模型的属性就足够了,然后只需通过以下方式设置控件的数据源即可:

radioButtonList1.DataSource = Enum.GetValues(typeof(YourEnumType)); 

标签:radiobuttonlist,data-binding,c,winforms,radio-button
来源: https://codeday.me/bug/20191111/2023218.html