如何设置combox项的可见性? [英] How to set combox item visibility?

查看:215
本文介绍了如何设置combox项的可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有相同组合框项目(Apple& Orange)的WPF组合框(comboboxA,comboboxB)。让我们说在组合框A中选择Apple,然后Apple需要隐藏在comboxB中。如果我回到comboxA并选择橙色,苹果将可见,橙色需要隐藏。我如何实现使用C#?



代码片段xaml:

 < ComboBox Name =comboboxA> 
< ComboBoxItem Content =AppleName =AppleA>< / ComboBoxItem>
< ComboBoxItem Content =OrangeName =OrangeA>< / ComboBoxItem>
< / ComboBox>

< ComboBox Name =comboboxB>
< ComboBoxItem Content =AppleName =AppleB>< / ComboBoxItem>
< ComboBoxItem Content =OrangeName =OrangeB>< / ComboBoxItem>
< / ComboBox>


解决方案

您可以使用sa_ddam213提到的方法,

  private void comboboxA_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
for(int i = 0; i <= comboboxB.Items.Count -1; i ++)
{
if((ComboBoxItem)(comboboxB.Items [i])) .Content.ToString()==((ComboBoxItem)comboboxA.SelectedItem).Content.ToString())
{
(ComboBoxItem)(comboboxB.Items [i]))。 Windows.Visibility.Collapsed;
}
else
((ComboBoxItem)(comboboxB.Items [i]))。Visibility = System.Windows.Visibility.Visible;
}
}


i have 2 WPF comboboxes(comboboxA, comboboxB)with identical combobox item(Apple & Orange). Let say i select "Apple" in the comboboxA, then the "Apple" need to be hidden in the comboxB. If i go back to comboxA and select "Orange", "Apple" will be visible and "Orange" need to be hidden. How can i achieve that using C#?

code snippet for xaml:

    <ComboBox Name="comboboxA" >
        <ComboBoxItem Content="Apple" Name="AppleA"></ComboBoxItem>
        <ComboBoxItem Content="Orange" Name="OrangeA"></ComboBoxItem>
    </ComboBox>

    <ComboBox Name="comboboxB" >
        <ComboBoxItem Content="Apple" Name="AppleB"></ComboBoxItem>
        <ComboBoxItem Content="Orange" Name="OrangeB"></ComboBoxItem>
    </ComboBox>

解决方案

You can use the Method that sa_ddam213 mentioned, or you can just brute force it in the SelectionChanged Event like so.

private void comboboxA_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    for (int i = 0; i <= comboboxB.Items.Count -1; i++)
    {
        if (((ComboBoxItem)(comboboxB.Items[i])).Content.ToString() == ((ComboBoxItem)comboboxA.SelectedItem).Content.ToString())
        {
            ((ComboBoxItem)(comboboxB.Items[i])).Visibility = System.Windows.Visibility.Collapsed;
        }
        else
            ((ComboBoxItem)(comboboxB.Items[i])).Visibility = System.Windows.Visibility.Visible;
    }
}

这篇关于如何设置combox项的可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆