2014-10-09 28 views
0

業務規則: 可以通過開幕式填充展示位置,其中可以分配與開幕相匹配的候選人。 開幕式需要一項資格。 候選人可以有許多資格和許多候選人可以獲得資格,所以聯合證書是爲了避免多對多關係。WPF實體的列表框的MVVM內容取決於實體的另一個列表框

問題: 主窗口顯示資格,候選人和安置。爲了添加展示位置,我點擊具有PlacementModel的DataContext的添加展示位置窗口,並出現一個新窗口,其中有2個列表框和一個用於完成添加的按鈕,用戶必須在列表框和候選列表中選擇一個展開位置,匹配出現在另一個列表框中。 2列表框的綁定路徑分別是「開口」和「證書」。 我想讓證書列表框更新其列表,每當我點擊一個開放。

實體是候選人,資格,安置,證書和開幕。

我該如何執行此操作?

列表框綁定路徑:

LstOpenings(的ItemsSource =開口的SelectedItem = SelectedOpening)

LstCertificates(的ItemsSource =證書,的SelectedItem = SelectedCertificate)

PlacementModel:

#region Public Interface 
public ObservableCollection<OpeningModel> Openings { get; private set; } 
public ObservableCollection<CertificateModel> Certificates { get; private set; } 
public OpeningModel SelectedOpening { get { return _selectedOpening; } set { _selectedOpening = value; } } 

public CertificateModel SelectedCertificate 
{ 
    get { return _selectedCertificate; } 
    set 
    { _selectedCertificate = value; 
    } 
} 

#endregion 

#region Private Helper 
private void GetOpenings() 
{`enter code here` 

    var all = _context.Openings.OrderBy(cust => cust.OpeningDescription).ToList() 
     .Select(
      opening => 
       new OpeningModel(opening, _context, _openingRepository, _companyRepository, 
        _qualificationRepository,_certificateRepository)); 
    Openings = new ObservableCollection<OpeningModel>(all); 
    ICollectionView view = CollectionViewSource.GetDefaultView(Openings); 
    view.SortDescriptions.Add(new SortDescription("OpeningDescription",ListSortDirection.Ascending)); 

} 


private void GetCertificates() 
{ 

    if (_selectedOpening == null) 
    { 
     Certificates = new ObservableCollection<CertificateModel>(); 

    } 
    else 
    { 
     var all =_certificateRepository.GetCertificates().Where(c => c.QualificationCode == _selectedOpening.QualificationCode) 
         .OrderBy(c => c.Qualification.QualificationDescription) 
         .ToList().Select(c=>new CertificateModel(c,_context,_certificateRepository,_candidateRepository,_qualificationRepository)); 
     Certificates = new ObservableCollection<CertificateModel>(all); 
     ICollectionView view = CollectionViewSource.GetDefaultView(Certificates); 
     view.SortDescriptions.Add(new SortDescription("QualificationDescription", ListSortDirection.Descending)); 
    } 
} 
#endregion 

回答

0

添加一種名爲OpeningOpeningModel類型的新房產和數據綁定t輸出給ListBox.SelectedItem屬性:

<ListBox ItemsSource="{Binding Openings}" SelectedItem="{Binding Opening}" ... /> 

現在,所有你需要做的就是每當Opening值改變更新Certificates集合。你可以做到這一點從Opening屬性setter:

public OpeningModel Opening 
{ 
    get { return opening; } 
    set 
    { 
     opening = value; 
     NotifyPropertyChanged("Opening"); 
     UpdateCertificates(); 
    } 
} 

我會讓你來完成UpdateCertificates方法,因爲我敢肯定你知道該怎麼做。

+0

對不起,但我不知道我應該在UpdateCertificates()裏面放什麼。 我嘗試使用我的GetCertificates()方法內的代碼,但假設選擇的開放永遠不會爲空,所以如果條件不再適用。這種方法不起作用,所以我真的不知道應該在UpdateCertificates中放置什麼,這會使得證書的列表框在每次單擊開始時篩選出不合格的候選人 – 2014-10-09 12:50:48