2010-03-19 4 views
5

Se ho tre pulsanti di opzione, qual è il modo migliore per collegarli a un enum che ha le stesse scelte? per esempio.Winform Bind Enum to Radio Buttons

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

public enum MyChoices 
{ 
    Choice1, 
    Choice2, 
    Choice3 
} 

risposta

1

So che questa è una domanda precedente, ma è stata la prima a comparire nei risultati di ricerca. Ho trovato un modo generico per legarsi pulsanti di opzione per un enum, o anche una stringa o un numero, ecc

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

E allora o sul costruttore del modulo o di carico di maschera, lo chiamano su ciascuno dei vostri Controlli RadioButton. dataSource è l'oggetto che contiene la proprietà enum. Mi sono assicurato che l'interfaccia INotifyPropertyChanged abbia implementato l'interfaccia INotifyPropertyChanged nell'enumerazione delle proprietà enum.

AddRadioCheckedBinding(Choice1RadioButton, dataSource, "MyChoice", MyChoices.Choice1); 
AddRadioCheckedBinding(Choice2RadioButton, dataSource, "MyChoice", MyChoices.Choice2); 
2

Potrebbe creare tre proprietà booleane:

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

quindi associare alle MyChoice_Choice1 e gli altri?

0

Ho solo pensato di aggiungere il modo in cui lo sto attualmente facendo. Non esiste un "legame" in quanto tale, ma si spera che faccia lo stesso lavoro.

commenti benvenuto.

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