I have 2 listboxes in my XAML and both of them are multi select. I have synchronized the selectedItems of the listboxes using Samuel Jack’s solution.
<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"/>
The lbSite needs to be populated depending on the values selected in lbOrg. So, in order to let the ViewModel know about the selection changed for lbOrg, I am handling the selection changed event for the Selected Items list for lbOrg and calling a method to populate the values for 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);
}
}
}
}
}
And I have defined the properties as:
public ObservableCollection<object> SelectedOrg
{
get
{
return _selectedOrg;
}
set
{
_selectedOrg = value;
OnPropertyChanged("SelectedOrg");
}
}
public ObservableCollection<object> AvailableSites
{
get
{
return _availableSites;
}
set
{
_availableSites = value;
OnPropertyChanged("AvailableSite");
}
}
My ViewModelBase implements the INotifyPropertyChanged. Also, the properties are defined as ObservableCollection where the object has been cast from seperate custom classes in both the properties.
I guess this should have been easy, but due to some strange reason, the listbox is not getting notified about the property getting changed and hence no change to the source is getting reflected on the view. I have bound an eventHandler with the Selection changed event of the lbOrg in the code behind to check if the Itemssource for lbSite gets updated, but it doesn’t though the AvailableSites property had the values required.
Any help here would be greatly appreciated.
You’ve missed an “s”. 🙂
should be
I have some code in my ViewModelBase that checks the string when in debug mode, it’s quite helpful for catching such errors:
There are also various elegant solutions knocking around for removing the reliance on magic strings altogether for
INotifyPropertyChanged, but I’ve not got around to using any of these yet.