c# - 如何用干净的代码处理大量单选按钮?
问题描述
页面上有很多组单选按钮,当您单击第一个单选按钮时,您会在字符串中添加一个,依此类推。如果一组单选按钮没有可点击的选项,会弹出一个对话框,如何重构这段复杂的代码?</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"); }
解决方案
这不是最迷人的解决方案,但使用了一些更好的技术,WPF
例如MultiBinding
和Converters
。该解决方案使用较少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();
}
}
}
推荐阅读
- flutter - 如何在颤动中创建 Instagram 不断变化的渐变背景
- c# - 将项目 A 中的配置值读入项目 B
- sql - 如何在不多次查询 max(column_name)+1 的情况下在表中插入行?
- javascript - 检索频道标识符的问题 - Discord.JS
- reactjs - 如何使用 MUI-Data-Tables 呈现列内的项目数组?
- firefox-addon - 如何在扩展清单中匹配 Firefox 的 about 页面?
- python - 使用 Python 从 XML 文件中获取文件 url
- html - 如何在 Xaringan 演示文稿中调整图像大小?
- java - javafx Metro 主题 - 表格视图颜色文本
- matlab - 如何在 Matlab 中绘制“多维”图形。(即每个点代表一个混淆 2x2 矩阵)