Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Winforms将枚举绑定到单选按钮_C#_Winforms_Data Binding_Enums_Radio Button - Fatal编程技术网

C# Winforms将枚举绑定到单选按钮

C# Winforms将枚举绑定到单选按钮,c#,winforms,data-binding,enums,radio-button,C#,Winforms,Data Binding,Enums,Radio Button,如果我有三个单选按钮,那么将它们绑定到具有相同选项的枚举的最佳方法是什么?e、 g [] Choice 1 [] Choice 2 [] Choice 3 public enum MyChoices { Choice1, Choice2, Choice3 } 能否创建三个布尔属性: private MyChoices myChoice; public bool MyChoice_Choice1 { get { return myChoice == MyCho

如果我有三个单选按钮,那么将它们绑定到具有相同选项的枚举的最佳方法是什么?e、 g

[] Choice 1
[] Choice 2
[] Choice 3

public enum MyChoices
{
    Choice1,
    Choice2,
    Choice3
}

能否创建三个布尔属性:

private MyChoices myChoice;

public bool MyChoice_Choice1
{
    get { return myChoice == MyChoices.Choice1; }
    set { if (value) myChoice = MyChoices.Choice1; myChoiceChanged(); }
}

// and so on for the other two

private void myChoiceChanged()
{
    OnPropertyChanged(new PropertyChangedEventArgs("MyChoice_Choice1"));
    OnPropertyChanged(new PropertyChangedEventArgs("MyChoice_Choice2"));
    OnPropertyChanged(new PropertyChangedEventArgs("MyChoice_Choice3"));
}

然后绑定到MyChoice\u Choice1和其他选项?

我只是想添加我目前正在使用的方法。没有这样的“绑定”,但希望它也能起到同样的作用

欢迎评论

public enum MyChoices
{
    Choice1,
    Choice2,
    Choice3
}

public partial class Form1 : Form
{
    private Dictionary<int, RadioButton> radButtons;
    private MyChoices choices;

    public Form1()
    {
        this.InitializeComponent();
        this.radButtons = new Dictionary<int, RadioButton>();
        this.radButtons.Add(0, this.radioButton1);
        this.radButtons.Add(1, this.radioButton2);
        this.radButtons.Add(2, this.radioButton3);

        foreach (var item in this.radButtons)
        {
            item.Value.CheckedChanged += new EventHandler(RadioButton_CheckedChanged);
        }
    }

    private void RadioButton_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton button = sender as RadioButton;
        foreach (var item in this.radButtons)
        {
            if (item.Value == button)
            {
                this.choices = (MyChoices)item.Key;
            }
        }
    }

    public MyChoices Choices
    {
        get { return this.choices; }
        set
        {
            this.choices = value;
            this.SelectRadioButton(value);
            this.OnPropertyChanged(new PropertyChangedEventArgs("Choices"));
        }
    }

    private void SelectRadioButton(MyChoices choiceID)
    {
        RadioButton button;
        this.radButtons.TryGetValue((int)choiceID, out button);
        button.Checked = true;
    }
}
公共枚举MyChoices
{
选择1,
选择2,
选择3
}
公共部分类Form1:Form
{
专用字典按钮;
私人选择;
公共表格1()
{
this.InitializeComponent();
this.radButtons=新字典();
this.radButtons.Add(0,this.radioButton1);
this.radButtons.Add(1,this.radioButton2);
此.radButtons.Add(2,此.radioButton3);
foreach(此.radButtons中的变量项)
{
item.Value.CheckedChanged+=新事件处理程序(RadioButton\u CheckedChanged);
}
}
私有void单选按钮\u CheckedChanged(对象发送方,事件参数e)
{
RadioButton button=发送方为RadioButton;
foreach(此.radButtons中的变量项)
{
如果(item.Value==按钮)
{
this.choices=(MyChoices)item.Key;
}
}
}
公共选择
{
获取{返回this.choices;}
设置
{
这个选项=值;
选择RadioButton(值);
此.OnPropertyChanged(新PropertyChangedEventArgs(“选择”);
}
}
private void SelectRadioButton(MyChoices choiceID)
{
单选按钮;
this.radButtons.TryGetValue((int)choiceID,out按钮);
按钮。选中=真;
}
}

我知道这是一个老问题,但它是第一个出现在我的搜索结果中的问题。我找到了一种将单选按钮绑定到枚举、甚至字符串或数字等的通用方法

    private void AddRadioCheckedBinding<T>(RadioButton radio, object dataSource, string dataMember, T trueValue)
    {
        var binding = new Binding(nameof(RadioButton.Checked), dataSource, dataMember, true, DataSourceUpdateMode.OnPropertyChanged);
        binding.Parse += (s, a) => { if ((bool)a.Value) a.Value = trueValue; };
        binding.Format += (s, a) => a.Value = ((T)a.Value).Equals(trueValue);
        radio.DataBindings.Add(binding);
    }

问题海报没有提到他与什么有关。我想他是绑定到XXX.Properties.Settings类的。否则,此解决方案需要绑定类来实现INotifyPropertyChanged接口

并且有这样一个索引器:

public对象此[string propertyName]{get;set;}
我尝试了WeskerTyrant提供的公认解决方案,但失败了

我必须点击两次才能工作

所以我开始自己解决这个问题。我注意到我绑定到的类实现了INotifyPropertyChanged接口。它有一个名为PropertyChanged的PropertyChangedEventHandler

(顺便说一下,我正在使用.NET472) 所以我自己写了一个助手类

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Windows.Forms;
namespace Ezfx.Dui
{
    public static class RadioButtonGroupUI
    {
        public static void Apply(ApplicationSettingsBase settings, string key, Control parentControl)
        {
            List<RadioButton> radios = new List<RadioButton>();
            foreach(Control control in parentControl.Controls)
            {
                switch (control)
                {
                    case RadioButton e:
                        radios.Add(e);
                        break;
                }
            }

            Apply(settings, key, radios.ToArray());
        }

        public static void Apply(ApplicationSettingsBase settings, string key, params RadioButton[] radios)
        {

            foreach (RadioButton radioButton in radios)
            {
                if (radioButton.Text == (string)settings[key])
                {
                    radioButton.Checked = true;
                }

                radioButton.CheckedChanged += (sender, e) => {
                    if (radioButton.Checked)
                    {
                        settings[key] = radioButton.Text;
                    }
                    
                };
            }

            settings.PropertyChanged += (sender, e) =>
            {
                foreach (RadioButton radioButton in radios)
                {
                    if (radioButton.Text == (string)settings[key])
                    {
                        radioButton.Checked = true;
                    }
                    else
                    {
                        radioButton.Checked = false;
                    }
                }
            };
        }
    }
}
我在github中分享了这一点:

我想人们会投票否决我,因为这并不能回答有关enum的问题。我的解决方案是在使用时强制转换设置。在我的例子中,我使用python脚本中的设置,因此不需要强制转换

Enum.TryParse(settings.category, out Category category);
Enum.TryParse(settings.category, out Category category);