私はWPFでComboBoxの配列のコードを書いています。 アプリケーションは、選択したコードに基づいて楽譜を検索します。コードを単純化する:UIコントロールのifステートメント
左側のコンボボックスには、プレフィックス "tb"(トーンボックス)、右側の "cb"(コードボックス)が付いています。
Two columns and three rows of ComboBoxes with a "Search" button on the bottom コンボボックスのすべて
はありませんSelectionChangedイベントハンドラを持って、次の- ComboBoxの選択した値が0でない場合(「 - なし - 」)、以下のすべてのコンボボックスにすべての場合
- を無効にします「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文のこの木を簡素化することができますか? コードはうまくいきますが、何かよりもはるかに優れた書き方ができます。
ありがとうございました!
ありがとうございました!しかし、あなたの "cb"ソリューションは私が念頭に置いたものではなく、完全に説明していない間違いです。 1つのコードに対して一致するスケールを見つけることができます - 行の中の "cb"をすべて "cb"ではなく選択する必要があります。 –
@DenisAkopyan編集された答えを見る – Sweeper