简化代码:if用于UI控件的语句

时间:2017-01-07 14:11:43

标签: c# wpf user-interface combobox simplify

我正在为WPF中的ComboBox数组编写代码。 应用程序根据选定的和弦搜索音阶。

左侧的组合框以“tb”(音调框)为前缀,右侧为“cb”(和弦框)。

Two columns and three rows of ComboBoxes with a "Search" button on the bottom 所有ComboBox都有一个SelectionChanged事件处理程序,它执行以下操作:

  • 如果ComboBox选择的值为0(“ - None - ”),则禁用所有以下ComboBoxes
  • 如果未选择所有“cb”ComboBox,请禁用“搜索”按钮

以下是事件处理程序的代码:

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 语句的这个树? 代码有效,但有些东西告诉我写得好得多。

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您需要使用else if语句并逐步解决问题。

  

如果ComboBox选择的值为0(&#34; - None - &#34;),则禁用以下所有组合框

相当不言自明:

searchBtn.IsEnabled = false;
if (tbOne.SelectedIndex == 0) {
    cbOne.IsEnabled = false;
    tbTwo.IsEnabled = false;
    cbTwo.IsEnabled = false;
    tbThree.IsEnabled = false;
    cbThree.IsEnabled = false;
} else if (cbOne.SelectedIndex == 0) {
    tbTwo.IsEnabled = false;
    cbTwo.IsEnabled = false;
    tbThree.IsEnabled = false;
    cbThree.IsEnabled = false;
} else if (tbTwo.SelectedIndex == 0) {
    cbTwo.IsEnabled = false;
    tbThree.IsEnabled = false;
    cbThree.IsEnabled = false;
} else if (cbTwo.SelectedIndex == 0) {
    tbThree.IsEnabled = false;
    cbThree.IsEnabled = false;
} else if (tbThree.SelectedIndex == 0) {
    cbThree.IsEnabled = false;
}
  

如果所有&#34; cb&#34;未选择ComboBox,禁用搜索按钮

我们可以将其改为

  

如果&#34; cb&#34;选择ComboBox,启用搜索按钮:

if (cbOne.SelectedIndex != 0 || cbTwo.SelectedIndex != 0 || cbThree.SelectedIndex != 0) {
    searchBtn.IsEnabled = true;
}