如何设置组合框项目的可见性?

2024-01-22

我有 2 个 WPF 组合框(组合框A、组合框B),具有相同的组合框项目(Apple 和 Orange)。假设我在组合框A中选择“Apple”,那么“Apple”需要隐藏在组合框B中。如果我返回组合框 A 并选择“Orange”,“Apple”将可​​见,而“Orange”需要隐藏。我如何使用 C# 来实现这一点?

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>

您可以使用 sa_ddam213 提到的方法,或者您可以像这样在 SelectionChanged 事件中暴力破解它。

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;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何设置组合框项目的可见性? 的相关文章

随机推荐