2010-03-19 30 views
5

Nếu tôi có ba nút radio, cách tốt nhất để liên kết chúng với một enum có cùng lựa chọn là gì? ví dụ.Winforms Bind Enum to Radio Buttons

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

public enum MyChoices 
{ 
    Choice1, 
    Choice2, 
    Choice3 
} 

Trả lời

1

Tôi biết đây là câu hỏi cũ nhưng đây là câu hỏi đầu tiên hiển thị trong kết quả tìm kiếm của tôi. Tôi đã tìm ra một cách chung chung để ràng buộc nút radio để một enum, hoặc thậm chí một chuỗi hoặc số vv

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

Và sau đó, hoặc vào constructor của biểu mẫu hoặc trên các sự kiện hình thức nạp, gọi nó là trên mỗi bạn RadioButton điều khiển. dataSource là đối tượng chứa thuộc tính enum của bạn. Tôi đảm bảo rằng dataSource đã triển khai giao diện INotifyPropertyChanged đang kích hoạt sự kiện OnPropertyChanged trong trình cài đặt thuộc tính enum.

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

bạn có thể tạo ra ba thuộc tính boolean:

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

sau đó liên kết với MyChoice_Choice1 và những người khác?

0

Tôi chỉ nghĩ rằng tôi sẽ thêm cách mà tôi hiện đang thực hiện. Không có 'ràng buộc' như vậy nhưng hy vọng nó làm cùng một công việc.

Nhận xét chào mừng.

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