2010-03-19 12 views
5

Si tengo tres botones de opción, ¿cuál es la mejor manera de vincularlos a una enumeración que tiene las mismas opciones? p.ej.Winforms Enlazar botones de Enum a Radio

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

public enum MyChoices 
{ 
    Choice1, 
    Choice2, 
    Choice3 
} 

Respuesta

1

Sé que esta es una pregunta anterior, pero fue la primera en aparecer en mis resultados de búsqueda. Me di cuenta de una forma genérica para unirse botones de radio para una enumeración, o incluso una cadena o un número, etc.

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

Y a continuación, ya sea en el constructor de su forma o en el evento de carga del formulario, lo llaman en cada uno de sus RadioButton controles. El dataSource es el objeto que contiene su propiedad enum. Me aseguré de que dataSource implementado la interfaz INotifyPropertyChanged está activando el evento OnPropertyChanged en el conjunto de propiedades enum.

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

Podría crear tres propiedades booleanas:

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

continuación, se unen a MyChoice_Choice1 y los demás?

0

Pensé que agregaría la forma en que lo hago actualmente. No hay 'vinculante' como tal, pero con suerte hace el mismo trabajo.

Comentarios bienvenidos.

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