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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13557942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:21:28  来源:igfitidea点击:

How to set combox item visibility?

c#wpfcombobox

提问by 0070

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

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

code snippet for xaml:

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>

采纳答案by Mark Hall

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

您可以使用 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;
    }
}

回答by sa_ddam213

Maybe just filtering the selected item from list

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

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

Or do you need this to work both ways

或者你需要这个来双向工作