【发布时间】:2017-01-07 14:11:43
【问题描述】:
我正在为 WPF 中的 ComboBoxes 数组编写代码。 该应用程序根据选定的和弦搜索音乐音阶。
左侧的 ComboBox 以“tb”(音框)为前缀,右侧的 ComboBox 以“cb”(和弦框)为前缀。
Two columns and three rows of ComboBoxes with a "Search" button on the bottom 所有的 ComboBox 都有一个 SelectionChanged 事件处理程序,它执行以下操作:
- 如果 ComboBox 选择的值为 0(“- 无 -”),则禁用所有后续 ComboBox
- 如果未选中所有“cb”组合框,请禁用搜索按钮
这是事件处理程序的代码:
btnSearch.IsEnabled = false;
if (tbOne.SelectedIndex != 0)
{
cbOne.IsEnabled = true;
if (cbOne.SelectedIndex != 0)
{
btnSearch.IsEnabled = true;
tbTwo.IsEnabled = true;
if (tbTwo.SelectedIndex != 0)
{
btnSearch.IsEnabled = false;
cbTwo.IsEnabled = true;
if (cbTwo.SelectedIndex != 0)
{
btnSearch.IsEnabled = true;
tbThree.IsEnabled = true;
if (tbThree.SelectedIndex != 0)
{
btnSearch.IsEnabled = false;
cbThree.IsEnabled = true;
if (cbThree.SelectedIndex != 0)
{
btnSearch.IsEnabled = true;
}
else
{
btnSearch.IsEnabled = false;
}
}
else
{
btnSearch.IsEnabled = true;
cbThree.IsEnabled = false;
}
}
else
{
btnSearch.IsEnabled = false;
tbThree.IsEnabled = false;
cbThree.IsEnabled = false;
}
}
else
{
btnSearch.IsEnabled = true;
cbTwo.IsEnabled = false;
tbThree.IsEnabled = false;
cbThree.IsEnabled = false;
}
}
else
{
btnSearch.IsEnabled = false;
tbTwo.IsEnabled = false;
cbTwo.IsEnabled = false;
tbThree.IsEnabled = false;
cbThree.IsEnabled = false;
}
}
else
{
cbOne.IsEnabled = false;
tbTwo.IsEnabled = false;
cbTwo.IsEnabled = false;
tbThree.IsEnabled = false;
cbThree.IsEnabled = false;
}
如你所见,它看起来并不漂亮。
这是 UI 的 XAML:
<Grid Width="200">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<ComboBox Name="tbOne" KeyboardNavigation.TabIndex="1" Margin="0,10,2.5,0" SelectedIndex="0" SelectionChanged="selectionChanged" IsEnabled="True">
<ComboBoxItem Content="- None -"/>
</ComboBox>
<ComboBox Name="tbTwo" KeyboardNavigation.TabIndex="3" Margin="0,10,2.5,0" SelectedIndex="0" SelectionChanged="selectionChanged" IsEnabled="False">
<ComboBoxItem Content="- None -"/>
</ComboBox>
<ComboBox Name="tbThree" KeyboardNavigation.TabIndex="5" Margin="0,10,2.5,0" SelectedIndex="0" SelectionChanged="selectionChanged" IsEnabled="False">
<ComboBoxItem Content="- None -"/>
</ComboBox>
</StackPanel>
<StackPanel Grid.Column="1">
<ComboBox Name="cbOne" KeyboardNavigation.TabIndex="2" Margin="2.5,10,0,0" SelectedIndex="0" SelectionChanged="selectionChanged" IsEnabled="False">
<ComboBoxItem Content="- None -"/>
</ComboBox>
<ComboBox Name="cbTwo" KeyboardNavigation.TabIndex="4" Margin="2.5,10,0,0" SelectedIndex="0" SelectionChanged="selectionChanged" IsEnabled="False">
<ComboBoxItem Content="- None -"/>
</ComboBox>
<ComboBox Name="cbThree" KeyboardNavigation.TabIndex="6" Margin="2.5,10,0,0" SelectedIndex="0" SelectionChanged="selectionChanged" IsEnabled="False">
<ComboBoxItem Content="- None -"/>
</ComboBox>
</StackPanel>
</Grid>
<Button Content="Search" Margin="0, 10, 0,0" IsEnabled="False" Name="btnSearch"/>
所以我的问题是如何简化这个 if 语句树? 代码有效,但有些事情告诉我它可以写得更好。
谢谢!
【问题讨论】:
标签: c# wpf user-interface combobox simplify