首页 > 解决方案 > 如何用干净的代码处理大量单选按钮?

问题描述

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

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

标签: c#wpf

解决方案


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

推荐阅读