0

同じ 3 つの項目 (a、b、c) を持つ 3 つのコンボ ボックスがあります。コンボボックス1で「a」を選択すると、「a」がコンボボックス2から削除され、コンボボックス2に残っているアイテムは「b」と「c」になります。そして、コンボボックス2で「b」を選択すると、「b」はコンボボックス3から削除され、コンボボックス3のアイテムは「a」と「c」になります。以前のコンボボックスが selectionChanged を通過した場合、削除されたアイテムは再びコンボボックスに復元されます。インターネットで見つけたいくつかのコードを試しましたが、機能しません...前のコンボボックスから選択したアイテムが削除されていません。

コンボ ボックスの私のコード:

<ComboBox Name="firstCombo" SelectionChanged="firstCombo_SelectionChanged">
    <ComboBoxItem Content="A"></ComboBoxItem>
    <ComboBoxItem Content="B"></ComboBoxItem>
    <ComboBoxItem Content="C"></ComboBoxItem>
</ComboBox>

<ComboBox Name="secondCombo" SelectionChanged="secondCombo_SelectionChanged">
    <ComboBoxItem Content="A"></ComboBoxItem>
    <ComboBoxItem Content="B"></ComboBoxItem>
    <ComboBoxItem Content="C"></ComboBoxItem>
</ComboBox>

<ComboBox Name="thirdCombo" >
    <ComboBoxItem Content="A"></ComboBoxItem>
    <ComboBoxItem Content="B"></ComboBoxItem>
    <ComboBoxItem Content="C"></ComboBoxItem>
</ComboBox>

私のC#コード:

private void firstCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    secondCombo.Items.Remove(firstCombo.SelectionBoxItem);         
}

private void secondCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    thirdCombo.Items.Remove(secondCombo.SelectionBoxItem);         
}
4

4 に答える 4

1

ComboBoxItem問題は、それらが実際には異なるインスタンスであることだと思います。それらは同じテキストを持っていますが、それでも異なるインスタンスです。そのため、SelectionBoxItemfromsecondComboは見つからthirdCombo.Itemsないため、削除されません。

表示されたテキストに基づいて削除する必要があります。

于 2012-11-23T08:58:44.277 に答える
0

アイテムを追加したり削除したりするのではなく、個々のアイテムの可視性を変更するだけです。

XAML で (コンバーターを介して) バインドすると、「削除」と「読み込み」が自動的に行われます。

<ComboBox Name="firstCombo">
    <ComboBoxItem Content="A"></ComboBoxItem>
    <ComboBoxItem Content="B"></ComboBoxItem>
    <ComboBoxItem Content="C"></ComboBoxItem>
</ComboBox>

<ComboBox Name="secondCombo">
    <ComboBoxItem Content="A"
                  Visiblity="{Binding ElementName=firstCombo Path=SelectedIndex,
                              Converter=IndexToVisibiltyConverter,
                              ConverterParameter=A></ComboBoxItem>
    <ComboBoxItem Content="B"
                  Visiblity="{Binding ElementName=firstCombo Path=SelectedIndex,
                              Converter=IndexToVisibiltyConverter,
                              ConverterParameter=B></ComboBoxItem>
    <ComboBoxItem Content="C"
                  Visiblity="{Binding ElementName=firstCombo Path=SelectedIndex,
                              Converter=IndexToVisibiltyConverter,
                              ConverterParameter=C></ComboBoxItem>
</ComboBox>

<ComboBox Name="thirdCombo">
    <ComboBoxItem Content="A"
                  Visiblity="{Binding ElementName=secondCombo Path=SelectedIndex,
                              Converter=IndexToVisibiltyConverter,
                              ConverterParameter=A></ComboBoxItem>
    <ComboBoxItem Content="B"
                  Visiblity="{Binding ElementName=secondCombo Path=SelectedIndex,
                              Converter=IndexToVisibiltyConverter,
                              ConverterParameter=B></ComboBoxItem>
    <ComboBoxItem Content="C"
                  Visiblity="{Binding ElementName=secondCombo Path=SelectedIndex,
                              Converter=IndexToVisibiltyConverter,
                              ConverterParameter=C></ComboBoxItem>
</ComboBox>

*注意: コンバーターの定義は正当な構文ではありません - 説明のみです!

表示されたテキストまたは選択した値にバインドできます-最も便利なものは何でも。

コンバーターは、パラメーターに対してインデックス/テキスト/値をチェックし、必要に応じてVisibility.VisibleorVisibility.Collapsedを返します。

于 2012-11-23T09:24:04.587 に答える
0

SelectedIndex を使用して削除できますが、以前に何かを削除したかどうかに注意してください。既に削除した場合、インデックスは同じではありません。

private void firstCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    secondCombo.Items.RemoveAt(firstCombo.SelectedIndex);         
}
于 2012-11-23T08:59:56.870 に答える
0

というか、たぶん。

String strCombo1 = comboBox1.SelectedItem.ToString(); 
comboBox2.Items.Remove(strCombo1);
于 2012-11-23T09:09:28.340 に答える