2012-11-26 5 views
4

나는 동일한 콤보 박스 항목 (Apple & 오렌지색)을 가진 2 개의 WPF 콤보 상자 (콤보 상자 A, 콤보 상자 B)를 가지고 있습니다. "comboboxA"에서 "Apple"을 선택한 다음 "apple"을 comboxB에 숨길 필요가 있다고 가정 해 보겠습니다. 내가 comboxA로 돌아가서 "Orange"를 선택하면 "Apple"이 보이고 "Orange"가 숨겨 질 필요가 있습니다. 어떻게 C#을 사용하여 그것을 달성 할 수 있습니까? XAML에 대한combox 아이템 가시성을 설정하는 방법?

코드 :

<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

두 가지 작업이 필요합니까 그 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; 
    } 
} 
+0

감사 마크, 당신도 잘 작동합니다 C =. 내가 또 다른 질문이있어. 내가 comboboxA에서 "apple"을 선택하면 "apple"이 comboxB에 숨겨집니다. 하지만 여전히 "사과"를위한 공백이 있습니다. 단순히 공백을 제거 할 수 있습니까? – 0070

+0

@ 0070 숨겨진 참조 대신 System.Windows.Visibility.Collapsed를 사용해보십시오 –

+0

감사 마크. +1 ^^ – 0070

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

또는 당신은 당신이 방법을 사용할 수 있습니다

+0

"ObservableCollection"을 사용하려면 참조를 추가해야합니까? – 0070

+0

예 : 'using System.Collections.ObjectModel; 아니면 그냥 목록으로 만들 수도 있지만 항목을 추가 한 후에 NotifyPropertyChanged ("YourCollection")를 호출해야합니다. Observable 컬렉션에는이 함수가 내장되어 있으며 항목을 추가/제거 할 때 UI가 업데이트되므로 WPF –

+0

hmmm .....에 대한 매우 편리한 목록 유형이므로 아무 것도 콤보 상자에 표시되지 않습니다. 두 콤보 상자를 모두 클릭하면 그냥 비어 있습니다. – 0070

관련 문제