【问题标题】:How to remove the Wpf combo box item using C#如何使用 C# 删除 Wpf 组合框项
【发布时间】:2012-11-23 08:52:12
【问题描述】:

我有 3 个组合框,其中包含相同的 3 个项目(a、b、c)。如果我在combobox1中选择“a”,“a”将从combobox2中删除,留在combobox2中的项目将是“b”和“c”。然后我在combobox2中选择“b”,“b”将从combobox3中删除,combobox3中的项目将是“a”和“c”。 如果前一个组合框通过 selectionChanged,则删除的项目将再次恢复到组合框中。我尝试了一些在 Internet 上找到的代码,但不起作用...从 previos 组合框中选定的项目没有被删除。

我的组合框代码:

<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);         
}

【问题讨论】:

  • 错误是什么?如果所有项目都已逐渐从组合框中删除怎么办?有什么要留下的吗?您如何继续您的计划?
  • “不起作用”不是错误描述。有什么问题?
  • 对不起...我忘了写出错误。我已经更新了这个问题。请再看看。谢谢。
  • To any prospective answerers - please note that OP has added the requirement that when the selection changes, unselected items are to be put back into the subsequent comboboxes.

标签: c# wpf combobox


【解决方案1】:

我想问题是这些实际上是不同的ComboBoxItem 实例。它们具有相同的文本,但它们仍然是不同的实例。因此,secondCombo 中的 SelectionBoxItem 不会在 thirdCombo.Items 中找到,因此不会被删除。

您需要根据显示的文字将其删除。

【讨论】:

  • 那么我该如何实现呢?
【解决方案2】:

您可以使用 SelectedIndex 删除它,但如果您之前删除过某些内容,请注意它,因为如果您已经删除它,那么索引就不一样了:

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

【讨论】:

  • 那行不通。在示例中,从第二个组合框中删除“A”后,“B”将具有索引 0。这反过来将从第三个组合框中删除“A”而不是“B”。
【解决方案3】:

也就是说,也许吧。

String strCombo1 = comboBox1.SelectedItem.ToString(); 
comboBox2.Items.Remove(strCombo1);

【讨论】:

    【解决方案4】:

    您可以只更改单个项目的可见性,而不是添加和删除项目。

    如果您在 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.VisibleVisibility.Collapsed

    【讨论】:

    • 抱歉回复晚了....当我尝试编译我的文件时出现 2 个错误。内部异常:从数字转换时,值必须小于无穷大。内部异常:确保源类型可转换为目标类型。
    • @0070 - 我确实说过转换器语法只是说明性的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多