也許只是從列表過濾選擇的項目
<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));
}
}
}
或者你需要這兩種方式工作
謝謝馬克,你的工作也很好C =。順便說一句,我有另一個問題。因爲我在comboboxA中選擇了「apple」,所以「apple」會隱藏在comboxB中。但是「蘋果」仍然有一個白色空間。我可以簡單地刪除白色空間嗎? – 0070
@ 0070嘗試使用'System.Windows.Visibility.Collapsed而不是隱藏請參閱編輯 –
謝謝馬克。 +1 ^^ – 0070