2013-06-21 59 views
0

我有兩個ComboBox
WPF應用程序。當我選擇的第一個與第一ComboBox中的項目將在第二個如何清除所選項目集合

填充這是我的選擇物業

public string SelectedApplication 
    { 
     set 
     { 
      if (_selectedApplication == value) return; 
      this._selectedApplication = value; 

      InitializeTransactionTypes(); 


     } 
     get 
     { 
      return this._selectedApplication; 
     } 

    } 

這裏我正在檢查兩個組合框之間的匹配ID以填充第二個組合框項目。

ObservableCollection<TransactionTypeViewModel> _transTypeObsList = new ObservableCollection<TransactionTypeViewModel>(); 
     private void InitializeTransactionTypes() 
    { 
     if (_selectedApplication != null) 
     { 

       var getAppCode = 
       ApplicationVModel.GetAllApplications() 
           .FirstOrDefault(apps => apps.Name == _selectedApplication); 

      var transTypeList = TransactionTypeVModel.GetAllViewModelTransTypes() 
                .Where(t => getAppCode != null && t.Id == getAppCode.Id); 

      transactionTypes = new ObservableCollection<TransactionTypeViewModel>(transTypeList); 

      NotifyPropertyChanged("TransactionTypes"); 
     } 
    } 

有關方法的更多信息:

VM的列表從模型

的列表映射
 public List<TransactionTypeViewModel> GetAllViewModelTransTypes() 
    { 
     TransactionTypeViewModels = TransactionTypeModel.GetAllTransactionTypes().Select(transType => new TransactionTypeViewModel 
     { 
      Id = transType.Id, 
      Name = transType.Name, 


     }) 
    .ToList(); 
     return TransactionTypeViewModels; 
    } 

可以說,我選擇第一組合框具有{A,B,C,d} ...第二個組合框具有{A'1,A'2,A'3},當我從第一個組合框中選擇項目時,第二個組合框將不斷填充 項目。我想只顯示{A'1 for A} {B'1 for B} ...等,但現在它所做的是{A'1 A'1 A'1 ... for A} {B' 1 B'1 B'1 ... for B}爲每個選擇。

我想先前的選擇被清除,並顯示一個新的列表每個選擇。謝謝

+0

而是在'InitializeTransactionTypes'嘗試'清除重建'_transTypeObsList'每次() '和'添加(...)'項目一個接一個。 – dkozl

+1

要清楚我不知道這是否是問題,但您經常創建您的列表。而是'transactionTypes = new ObservableCollection ....'做一些類似'transactionTypes.Clear(); foreach(....)transactionTypes.Add(....)' – dkozl

+0

@dkozl謝謝,這對我有用......我清除並添加了每一次迭代。 –

回答

0

總結評論。每次更改某個東西時都會重新創建一個已有的東西,而不是創建新的ObservableCollection,它會通知UI有關更改的信息。你應該InitializeTransactionTypes看起來是這樣的:

private void InitializeTransactionTypes() 
{ 
    if (_selectedApplication != null) 
    { 
     var getAppCode = 
      ApplicationVModel.GetAllApplications() 
          .FirstOrDefault(apps => apps.Name == _selectedApplication); 

     transactionTypes.Clear(); 
     foreach(var transactionItem in TransactionTypeVModel.GetAllViewModelTransTypes().Where(t => getAppCode != null && t.Id == getAppCode.Id)) 
     transactionTypes.Add(transactionItem); 
    } 
} 

像這樣,你不應該來通知TransactionTypes改變任何更多

+0

謝謝,這是一個很好的觀點 –

相關問題