2012-11-26 51 views
4

我有2個WPF組合框(comboboxA,comboboxB)具有相同的組合框項目(Apple &橙色)。假設我在組合框A中選擇了「Apple」,那麼「Apple」需要隱藏在comboxB中。如果我回到comboxA並選擇「橙色」,「蘋果」將可見,並且「橙色」需要隱藏。我怎樣才能實現使用C#?如何設置combox項目的可見性?

爲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

您可以使用方法提到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

是的,你需要添加: '使用System.Collections.ObjectModel;' 或者你可以只讓一個List ,但你會在添加項目後打電話到NotifyPropertyChanged(「YourCollection」)。 Observable集合內置了這個函數,並將在添加/刪除項目時更新UI,所以這是WPF的一個非常方便的列表類型 –

+0

hmmm .....沒有任何東西顯示在組合框中。當我點擊這兩個組合框時空白,請點擊這兩個組合框 – 0070

相關問題