Relativity
Relativity

Reputation: 6868

Change in Observable collection not reflected in UI

I have a proprty o type "Observable collection"...when i add an item it's not geeting reflected in UI ...what am doing wrong...?

<ComboBox Grid.Column="0" Grid.Row="3" 
Width="120" SelectedIndex="0"
Margin="5,0,0,0" HorizontalAlignment="Left"
ItemsSource="{Binding AllPlaces}" 
DisplayMemberPath="PlaceName" 
SelectedItem="{Binding Path=SelectedPlace.Value, Mode=TwoWay}" 
VerticalAlignment="Top">
</ComboBox>


// Add the new item to the existing place list, so that it will be refreshed.
ObservableCollection<PlaceDto> existingPlaceList = new ObservableCollection<PlaceDto>();
// Copy all places to a temperory list.
foreach(PlaceDto placeItem in AllPlaces)
{
existingPlaceList.Add(placeItem);
}
// Add new place to existing list
existingPlaceList .Add(newPlace);
AllPlaces= existingPlaceList;

Upvotes: 2

Views: 2776

Answers (1)

Joel Lucsy
Joel Lucsy

Reputation: 8706

The ObservableCollection will notify the GUI if the list changes. However, you're changing the entire list itself with the line AllDivisions = existingPlaceList. You'll have to implement INotifyPropertyChanged for the class that contains the AllDivisions property to tell the GUI when you swap out the list.

Upvotes: 5

Related Questions