WPF/C# - 编程创建和使用单选按钮的示例

7

请问有没有示例可以展示如何在C# WPF中以编程方式创建和使用单选按钮?

主要包括以下内容:(a) 如何以编程方式创建单选按钮,(b) 如何在值更改时捕获触发器,(c) 如何在特定时间获取结果。

如果答案基于绑定方法,也很感兴趣。如果数据绑定是最简单的方法,则提供一个示例会非常好。否则,如果数据绑定不是最佳/最简单的方法,则提供一个非数据绑定的示例会很好。

注意:

  • 请注意,我当前的父节点是StackPanel,因此问题的一个方面是如何向StackPanel添加多个RadioButton。

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

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

1个回答

16

通常,我们使用单选按钮(RadioButtons)向用户展示一个枚举( Enum) 数据类型。我们通常使用ItemsControl来呈现一组单选按钮,每个单选按钮都绑定到一个ViewModel。

下面是我刚写的一个示例应用程序,演示了单选按钮的两种用法:第一种是一种直接的方式(也许可以回答你上面的问题),第二种是使用MVVM模式的方式。

顺便说一句,这只是我快速编写的东西(是的,我手头有很多时间),所以我不会说这里的所有内容都是最完美的做法。但我希望你能找到这个有用:

XAML:

<Window x:Class="RadioButtonSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:RadioButtonSample"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <StackPanel x:Name="sp"/>
        <Button x:Name="showChoice" Click="showChoice_Click">Show Choice</Button>

        <StackPanel x:Name="sp2">
            <StackPanel.DataContext>
                <local:ViewModel/>
            </StackPanel.DataContext>
            <ItemsControl x:Name="itemsControl" ItemsSource="{Binding Path=Choices}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <RadioButton IsChecked="{Binding Path=IsChecked}" Content="{Binding Path=Choice}" GroupName="ChoicesGroup"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        <Button x:Name="showChoice2" Click="showChoice2_Click">Show Choice2</Button>
    </StackPanel>
</StackPanel>

代码后台:

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
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    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<ChoiceVM> Choices { get; private set; }
        public ViewModel()
        {
            this.Choices = new List<ChoiceVM>();

            //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

    }
}

谢谢 - 在我的情况下,我不会在编译时知道单选按钮的数量,因此我不确定枚举方法是否可行 - 有关这种情况应该怎么做的建议吗? - Greg
1
我相信它会以同样的方式工作。我认为你的RadioButton组中的项目将是某种集合。因此,在上面的代码中,只需替换迭代“Enum.GetValues(...)”的循环以使用您的集合即可。希望这有意义。 - ASanch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接