2012-03-27 118 views
1

我有2個列表框在我的XAML和他們都是多選。我已經使用Samuel Jack's solution同步了列表框的selectedItem。Listbox itemssource不更新與源

<ListBox x:Name="lbOrg" HorizontalAlignment="Left" Grid.Column="1" Grid.Row="0" 
Margin="15,3,5,3" Width="200" Height="120" ItemsSource="{Binding AvailableOrg}" 
LSync:MultiSelectorBehaviours.SynchronizedSelectedItems="{Binding SelectedOrg}" 
SelectionMode="Extended" DisplayMemberPath="OrgShortName"/> 


<ListBox x:Name="lbSite" HorizontalAlignment="Left" Grid.Column="3" Grid.Row="0" 
Margin="15,3,5,3" Width="200" Height="120" ItemsSource="{Binding AvailableSites}"  
LSync:MultiSelectorBehaviours.SynchronizedSelectedItems="{Binding SelectedSites}" 
SelectionMode="Extended" DisplayMemberPath="SiteShortName"/> 

lbSite需要根據在lbOrg選擇的值來進行填充。因此,爲了讓ViewModel知道更改爲lbOrg的選擇,我正在處理lbOrg的Selected Items列表的選擇更改事件,並調用一個方法來填充lbSite的值。

public void Load() 
    {    
     _selectedOrg = new ObservableCollection<object>(); 
     _selectedSites = new ObservableCollection<object>();    

     _selectedOrg.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_selectedOrg_CollectionChanged);    
    } 

    private void _selectedOrg_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
    {    
     if (AvailableSites != null) 
      AvailableSites.Clear();    

     if(SelectedOrg != null && SelectedOrg.Count > 0) 
     { 
      foreach (object org in SelectedOrg) 
      { 
       if (AvailableSites == null || AvailableSites.Count == 0) 
        AvailableSites = WebClient.getSitesForOrg(((OrganizationModel)org).OrgId); 
       else 
       { 
        ObservableCollection<object> tempSiteList = WebClient.getSitesForOrg(((OrganizationModel)org).OrgId); 
        foreach (object site in tempSiteList) 
        { 
         AvailableSites.Add(site); 
        } 
       } 
      } 
     } 
    } 

而且我所定義的屬性:

public ObservableCollection<object> SelectedOrg 
    { 
     get 
     { 
      return _selectedOrg; 
     } 
     set 
     { 
      _selectedOrg = value;     
      OnPropertyChanged("SelectedOrg"); 
     } 
    } 

public ObservableCollection<object> AvailableSites 
    { 
     get 
     { 
      return _availableSites; 
     } 
     set 
     { 
      _availableSites = value; 
      OnPropertyChanged("AvailableSite"); 
     } 
    } 

我ViewModelBase實現INotifyPropertyChanged的。另外,這些屬性被定義爲ObservableCollection,其中對象已經從兩個屬性中的單獨自定義類轉換而來。

我想這應該很容易,但由於一些奇怪的原因,列表框沒有得到有關屬性被更改的通知,因此沒有更改源視圖反映在視圖上。我已經綁定了一個eventHandler,在後面的代碼中使用lbOrg的Selection changed事件來檢查lbSite的Itemssource是否得到更新,但是雖然AvailableSites屬性具有所需的值,但它不會。

在這裏的任何幫助將不勝感激。

回答

2

你錯過了「s」。 :)

OnPropertyChanged("AvailableSite"); 

應該

OnPropertyChanged("AvailableSites"); 

我在ViewModelBase一些代碼,檢查在調試模式下當字符串,它是捕捉這樣的錯誤非常有用:

[Conditional("DEBUG")] 
private void CheckPropertyNameIsValid(string propertyName) 
{ 
    // INotifyPropertyChanged notifies via strings, so use 
    // this helper to check that the string is accurate in debug builds. 
    if (TypeDescriptor.GetProperties(this)[propertyName] == null) 
     throw new Exception("Invalid property name: " + propertyName); 
} 

有也是各種優雅的解決方案,爲了取消對魔術字符串的完全依賴INotifyPropertyChanged,但我還沒有得到使用任何這些。

+0

太棒了...抱歉浪費您的時間,我想知道我做了一切正確的事情。哇,從來沒有見過...謝謝噸... :) – 2012-03-27 10:21:19

+1

不用擔心,相信我我一直在那裏。 – GazTheDestroyer 2012-03-27 10:21:43