WPF / C# – 以编程方式创建和使用单选按钮的示例

有人能指出如何在C#WPF中以编程方式创建和使用单选按钮的示例吗?

所以基本上如何(a)以编程方式创建它们,以及(b)如何在值改变时捕获触发器,(c)如何在给定时间获取结果。

如果答案也将基于使用Binding方法,也会有兴趣看到。 如果数据绑定是最简单的方法,那么这个例子就会很棒。 否则,如果不需要数据绑定,那么最好/最简单的方法就是基于非数据绑定的示例。

笔记:

  • 请注意,我当前拥有的父节点是StackPanel,因此问题的一个方面是如何将多个RadioButtons添加到StackPanelI猜测。

  • 应该指出我不知道在编译时会有多少个单选按钮,也不知道在运行时会发现什么文本。

  • 它是一个WPF应用程序(即桌面,而不是Web应用程序)

通常,我们使用RadioButtons向用户呈现Enum数据类型。 我们通常做的是使用ItemsControl来呈现一组RadioButton,每一个都绑定到ViewModel。

下面是我刚刚编写的一个示例应用程序,演示了如何以两种方式使用RadioButtons:第一种是直接方法(这可能会回答上面的问题),第二种方法使用MVVM方法。

顺便说一下,这只是我写得很快的东西(是的,我手上有很多时间)所以我不会说这里的一切都是完美的做事方式。 但我希望你觉得这很有帮助:

XAML:

                  

代码隐藏:

 using System; using System.Collections; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.ComponentModel; using System.Collections.Generic; namespace RadioButtonSample { ///  /// Interaction logic for MainWindow.xaml ///  public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //Initialize the first group of radio buttons and add them to the panel. foreach (object obj in Enum.GetValues(typeof(ChoicesEnum))) { RadioButton rb = new RadioButton() { Content = obj, }; sp.Children.Add(rb); rb.Checked += new RoutedEventHandler(rb_Checked); rb.Unchecked += new RoutedEventHandler(rb_Unchecked); } } void rb_Unchecked(object sender, RoutedEventArgs e) { Console.Write((sender as RadioButton).Content.ToString() + " checked."); } void rb_Checked(object sender, RoutedEventArgs e) { Console.Write((sender as RadioButton).Content.ToString() + " unchecked."); } private void showChoice_Click(object sender, RoutedEventArgs e) { foreach (RadioButton rb in sp.Children) { if (rb.IsChecked == true) { MessageBox.Show(rb.Content.ToString()); break; } } } private void showChoice2_Click(object sender, RoutedEventArgs e) { //Show selected choice in the ViewModel. ChoiceVM selected = (sp2.DataContext as ViewModel).SelectedChoiceVM; if (selected != null) MessageBox.Show(selected.Choice.ToString()); } } //Test Enum public enum ChoicesEnum { Choice1, Choice2, Choice3, } //ViewModel for a single Choice public class ChoiceVM : INotifyPropertyChanged { public ChoicesEnum Choice { get; private set; } public ChoiceVM(ChoicesEnum choice) { this.Choice = choice; } private bool isChecked; public bool IsChecked { get { return this.isChecked; } set { this.isChecked = value; this.OnPropertyChanged("IsChecked"); } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propName) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propName)); } } #endregion } //Sample ViewModel containing a list of choices //and exposes a property showing the currently selected choice public class ViewModel : INotifyPropertyChanged { public List Choices { get; private set; } public ViewModel() { this.Choices = new List(); //wrap each choice in a ChoiceVM and add it to the list. foreach (var choice in Enum.GetValues(typeof(ChoicesEnum))) this.Choices.Add(new ChoiceVM((ChoicesEnum)choice)); } public ChoiceVM SelectedChoiceVM { get { ChoiceVM selectedChoice = this.Choices.FirstOrDefault((c) => c.IsChecked == true); return selectedChoice; } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propName) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propName)); } } #endregion } }