이 기능은 WPF에서 제공하지 않지만 일부 사용자 지정 코딩을 사용하여 구현할 수 있습니다.
내가 3 개 뷰 모델 클래스를 만들었습니다
PreferencesVM - 이것은 우리의 DataContext 될 것입니다. 여기에는 ComboBox에 나타날 수있는 옵션의 마스터 목록이 포함되어 있으며 다양한 ComboBox에서 선택된 항목을 추적하는 SelectedOptions 속성도 있습니다. 그것은 또한 ItemsControl.ItemsSource를 바인딩 할 Preferences 속성을 가지고 있습니다.
PreferenceVM - 이것은 하나의 ComboBox를 나타냅니다. ComboBox.SelectedItem이 바인딩 된 SelectedOption 속성이 있습니다. 또한 PreferencesVM에 대한 참조와 Options (ComboBox.ItemsSource가 바인딩 됨)라는 속성이 있으며 ComboBox에 항목이 표시 될 수 있는지 확인하는 필터를 통해 PreferencesVM에 대한 옵션을 반환합니다.
옵션 VM - ComboBox의 행을 나타냅니다. PreferenceVM.SelectedOption 설정하면
- (a ComboBoxItem 선택한 IE), 아이템이 PreferencesVM.AllOptions 컬렉션에 추가된다
다음 사항
용액에 키를 형성한다.
- PreferenceVM은 Preferences.SelectedItems.CollectionChanged를 처리하고 Options 속성에 대해 PropertyChanged를 발생시켜 새로 고침을 트리거합니다.
- PreferenceVM.Options는 필터를 사용하여 반환 할 항목을 결정합니다. 이는 PreferencesVM.SelectedOptions에없는 항목 만 허용합니다 (SelectedOption이 아닌 경우).
위에서 설명한 내용만으로도 충분하지만 두통을 줄이기 위해 아래 코드를 게시 할 것입니다.
PreferencesVM.cs :
public class PreferencesVM
{
public PreferencesVM()
{
PreferenceVM pref1 = new PreferenceVM(this);
PreferenceVM pref2 = new PreferenceVM(this);
PreferenceVM pref3 = new PreferenceVM(this);
this._preferences.Add(pref1);
this._preferences.Add(pref2);
this._preferences.Add(pref3);
//Only three ComboBoxes, but you can add more here.
OptionVM optRed = new OptionVM("Red");
OptionVM optGreen = new OptionVM("Green");
OptionVM optBlue = new OptionVM("Blue");
_allOptions.Add(optRed);
_allOptions.Add(optGreen);
_allOptions.Add(optBlue);
}
private ObservableCollection<OptionVM> _selectedOptions =new ObservableCollection<OptionVM>();
public ObservableCollection<OptionVM> SelectedOptions
{
get { return _selectedOptions; }
}
private ObservableCollection<OptionVM> _allOptions = new ObservableCollection<OptionVM>();
public ObservableCollection<OptionVM> AllOptions
{
get { return _allOptions; }
}
private ObservableCollection<PreferenceVM> _preferences = new ObservableCollection<PreferenceVM>();
public ObservableCollection<PreferenceVM> Preferences
{
get { return _preferences; }
}
}
PreferenceVM.cs :
public class PreferenceVM:INotifyPropertyChanged
{
private PreferencesVM _preferencesVM;
public PreferenceVM(PreferencesVM preferencesVM)
{
_preferencesVM = preferencesVM;
_preferencesVM.SelectedOptions.CollectionChanged += new NotifyCollectionChangedEventHandler(SelectedOptions_CollectionChanged);
}
void SelectedOptions_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this,new PropertyChangedEventArgs("Options"));
}
private OptionVM _selectedOption;
public OptionVM SelectedOption
{
get { return _selectedOption; }
set
{
if (value == _selectedOption)
return;
if (_selectedOption != null)
_preferencesVM.SelectedOptions.Remove(_selectedOption);
_selectedOption = value;
if (_selectedOption != null)
_preferencesVM.SelectedOptions.Add(_selectedOption);
}
}
private ObservableCollection<OptionVM> _options = new ObservableCollection<OptionVM>();
public IEnumerable<OptionVM> Options
{
get { return _preferencesVM.AllOptions.Where(x=>Filter(x)); }
}
private bool Filter(OptionVM optVM)
{
if(optVM==_selectedOption)
return true;
if(_preferencesVM.SelectedOptions.Contains(optVM))
return false;
return true;
}
public event PropertyChangedEventHandler PropertyChanged;
}
OptionVM.cs:
public class OptionVM
{
private string _name;
public string Name
{
get { return _name; }
}
public OptionVM(string name)
{
_name = name;
}
}
MainWindow.xaml.cs를 :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new PreferencesVM();
}
}
MainWindow를.xaml :
<Window x:Class="WpfApplication64.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">
<Grid>
<ItemsControl ItemsSource="{Binding Path=Preferences}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=Options}" DisplayMemberPath="Name" SelectedItem="{Binding Path=SelectedOption}"></ComboBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>
** 코드 줄을 줄이기 위해 제공된 솔루션은 10 개가 아닌 3 개의 ComboBox 만 생성합니다.
이 답변이 도움이 되었나요? 그렇다면 당신은 upvote 받아 받아야한다, 그렇지 않으면 우리가 잘못 된 부분을 알 수 있도록 몇 가지 피드백을 제공합니다. –