4

私は2つのWPFコンボボックス(コンボボックスA、コンボボックスB)を同一のコンボボックスアイテム(アップルとオレンジ)で持っています。コンボボックスAで「アップル」を選択すると、コンボボックスBで「アップル」を非表示にする必要があります。コンボ A に戻って「オレンジ」を選択すると、「アップル」が表示され、「オレンジ」を非表示にする必要があります。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>
4

2 に答える 2

4

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;
    }
}
于 2012-11-26T03:06:58.450 に答える
2

リストから選択したアイテムをフィルタリングするだけかもしれません

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

または、これが両方の方法で機能する必要がありますか

于 2012-11-26T02:54:52.343 に答える