繁体   English   中英

如何设置combox项目的可见性?

[英]How to set combox item visibility?

我有2个WPF组合框(comboboxA,comboboxB),它们具有相同的组合框项目(Apple和Orange)。 假设我在组合框A中选择了“ Apple”,则需要在comboxB中隐藏“ Apple”。 如果我回到comboxA并选择“橙色”,则“ Apple”将可​​见,并且“橙色”需要隐藏。 我如何使用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;
    }
}

也许只是从列表中过滤选定的项目

<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="UI">
    <Grid>
        <ComboBox ItemsSource="{Binding ElementName=UI,Path=YourCollection}" SelectedItem="{Binding ElementName=UI,Path=SelectedItem}" Height="23" HorizontalAlignment="Left" Margin="65,61,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
        <ComboBox ItemsSource="{Binding ElementName=UI, Path=FilteredCollection}" Height="23" HorizontalAlignment="Left" Margin="223,61,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" />
    </Grid>
</Window>


/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
    private string _selectedItem;
    private ObservableCollection<string> _yourCollection = new ObservableCollection<string>();

    public MainWindow()
    {
        InitializeComponent();
        YourCollection.Add("Apple");
        YourCollection.Add("Banana");
        YourCollection.Add("Pear");
        YourCollection.Add("Orange");
        NotifyPropertyChanged("FilteredCollection"); 
    }

    // Collection Fro ComboBox A
    public ObservableCollection<string> YourCollection
    {
        get { return _yourCollection; }
        set { _yourCollection = value; }
    }

    // ComboBox A selected Item
    public string SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;

            // Notify the the filter collection has changed
            NotifyPropertyChanged("FilteredCollection"); 
        }
    }

    // Collection to show in ComboBox B
    public List<string> FilteredCollection
    {
        // Remove the selected Item
        get { return _yourCollection.Where(s => !s.Equals(_selectedItem)).ToList(); }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

还是您需要这两种方式

暂无
暂无

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

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