简体   繁体   English

如何使用C#删除Wpf组合框项

[英]How to remove the Wpf combo box item using C#

i have 3 combo boxes with the same 3 items(a,b,c). 我有3个组合框,它们具有相同的3个项目(a,b,c)。 If i select "a" in combobox1, "a" will remove from combobox2, items left in combobox2 will be "b" & "c". 如果我在组合框1中选择“ a”,则“ a”将从组合框2中删除,组合框2中剩余的项将为“ b”和“ c”。 And then i select "b" in combobox2, "b" will remove from combobox3 and the item in combobox3 will be "a" & "c". 然后我在组合框2中选择“ b”,“ b”将从组合框3中删除,并且组合框3中的项目将为“ a”和“ c”。 The removed item will restore back again into the combo box if the previous combobox going through the selectionChanged. 如果先前的组合框经过selectionChanged,则删除的项目将再次恢复到组合框中。 I tried some codes i found on the internet, but doesnt work...the selected item from the previos combobox is not being removed. 我尝试了一些在Internet上找到的代码,但没有用...从previos组合框中选择的项目未被删除。

My code for combo boxes: 我的组合框代码:

<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>

my C# code: 我的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);         
}

I guess the problem is that those are actually different ComboBoxItem instances. 我想问题是这些实际上是不同的ComboBoxItem实例。 They have the same text but they are still different instances. 它们具有相同的文本,但仍然是不同的实例。 So, SelectionBoxItem from secondCombo will not be found in thirdCombo.Items and thus it won't be removed. 所以, SelectionBoxItemsecondCombo将不被发现thirdCombo.Items ,因此它不会被删除。

You need to remove it based on the displayed text. 您需要根据显示的文本将其删除。

You can use SelectedIndex to remove it but be aware of it if you have deleted something before or not because if you delete it already then the index isn't the same: 您可以使用SelectedIndex删除它,但是如果您之前是否删除过它,请注意它,因为如果您已经删除了它,那么索引就不一样了:

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

That to say, perhaps. 就是说。

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

Rather than adding and removing items you could just change the visibility of the individual items. 除了添加和删除项目外,您还可以更改各个项目的可见性。

If you bind it in the XAML (through a converter) the "removal" and "readding" would happen automatically. 如果将其绑定到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>

*NOTE: Converter definition not legal syntax - illustrative only! *注意:转换器定义不是合法语法-仅用于说明!

You could bind to the displayed text or selected value - whatever was most convenient. 您可以绑定到显示的文本或选定的值-最方便的方式。

The converter would check the index/text/value against the parameter and return Visibility.Visible or Visibility.Collapsed as appropriate. 转换器将根据参数检查索引/文本/值,并根据情况返回Visibility.VisibleVisibility.Collapsed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM