繁体   English   中英

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

[英]How to handle a large number of radiobuttons with clean code?

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

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

这不是最迷人的解决方案,但使用了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();
        }
    }
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM