如何用干净的代码处理大量的单选按钮?

时间:2019-01-30 02:37:03

标签: c# wpf

页面上有很多单选按钮组,单击第一个按钮时,将一个添加到字符串中,依此类推。如果一组单选按钮没有单击选项,则会弹出一个对话框,如何重构此复杂代码段?

        if (a121.IsChecked == true) { all1 += "1"; }
else    if (a122.IsChecked == true) { all1 += "2"; }
else    if (a123.IsChecked == true) { all1 += "3"; }
else    if (a124.IsChecked == true) { all1 += "4"; }
else    if (a125.IsChecked == true) { all1 += "5"; }
else
        { MessageBox.Show("An option is not selected"); }

        if (a131.IsChecked == true) { all1 += "1"; }
else    if (a132.IsChecked == true) { all1 += "2"; }
else    if (a133.IsChecked == true) { all1 += "3"; }
else    if (a134.IsChecked == true) { all1 += "4"; }
else    if (a135.IsChecked == true) { all1 += "5"; }
else
        { MessageBox.Show("An option is not selected"); }

        if (a141.IsChecked == true) { all1 += "1"; }
else    if (a142.IsChecked == true) { all1 += "2"; }
else    if (a143.IsChecked == true) { all1 += "3"; }
else    if (a144.IsChecked == true) { all1 += "4"; }
else    if (a145.IsChecked == true) { all1 += "5"; }
else
        { MessageBox.Show("An option is not selected"); }

2 个答案:

答案 0 :(得分:-1)

这不是最迷人的解决方案,但是使用了WPF中的一些更好的技术,例如MultiBindingConverters。该解决方案使用更少RadioButtons,但可以简单地改变。

XAML页

<Grid>
  <Grid.RowDefinitions>
     <RowDefinition Height="auto"/>
     <RowDefinition Height="auto"/>
     <RowDefinition Height="auto"/>
     <RowDefinition Height="auto"/>
  </Grid.RowDefinitions>

  <Label Margin="20">
     <Label.Content>
        <MultiBinding Converter="{StaticResource RadioButtonCustomStringConverter}">
           <Binding ElementName="i1r1" Path="IsChecked"/>
           <Binding ElementName="i1r2" Path="IsChecked"/>
           <Binding ElementName="i1r3" Path="IsChecked"/>
           <Binding ElementName="i2r1" Path="IsChecked"/>
           <Binding ElementName="i2r2" Path="IsChecked"/>
           <Binding ElementName="i2r3" Path="IsChecked"/>
           <Binding ElementName="i3r1" Path="IsChecked"/>
           <Binding ElementName="i3r2" Path="IsChecked"/>
           <Binding ElementName="i3r3" Path="IsChecked"/>
        </MultiBinding>
     </Label.Content>
  </Label>

  <StackPanel Grid.Row="1" Margin="20">
     <RadioButton x:Name="i1r1" GroupName="group1" Content="Option1"/>
     <RadioButton x:Name="i1r2" GroupName="group1" Content="Option2"/>
     <RadioButton x:Name="i1r3" GroupName="group1" Content="Option3"/>
  </StackPanel>

  <StackPanel Grid.Row="2" Margin="20">
     <RadioButton x:Name="i2r1" GroupName="group2" Content="Option1"/>
     <RadioButton x:Name="i2r2" GroupName="group2" Content="Option2"/>
     <RadioButton x:Name="i2r3" GroupName="group2" Content="Option3"/>
  </StackPanel>

  <StackPanel Grid.Row="3" Margin="20">
     <RadioButton x:Name="i3r1" GroupName="group3" Content="Option1"/>
     <RadioButton x:Name="i3r2" GroupName="group3" Content="Option2"/>
     <RadioButton x:Name="i3r3" GroupName="group3" Content="Option3"/>
  </StackPanel>
</Grid>

转换器

using System;
using System.Globalization;
using System.Windows.Data;
namespace WpfApplication2
{
    public class RadioButtonCustomStringConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if(values != null)
            {
                var result = "";
                for (int i = 0; i < values.Length; i++)
                    if (values[i] as bool? == true)
                        result += (i % 3);

                if (result.Length < 3)
                    return "You haven't selected three items.";
                else
                    return result;
            }
            return null;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

答案 1 :(得分:-2)

您可以定义以下方法来处理一组按钮:

private void AddOrMessage(RadioButton[] radioButtons)
{
    for (int i = 0; i < radioButtons.Length; i++)
    {
        if (radioButtons[i].IsChecked == true)
        {
            all1 += $"{i + 1}";
            return;
        }
    }

    MessageBox.Show("An option is not selected");
}

然后以以下方式使用它:

AddOrMessage(new [] {a121, a122, a123, a124, a125});
AddOrMessage(new [] {a131, a132, a133, a134, a135});
AddOrMessage(new [] {a141, a142, a143, a144, a145});