Observable集合中的更改未反映在UI中

时间:2010-11-15 00:16:51

标签: wpf data-binding observablecollection

我在“Observable collection”类型上有一个属性...当我添加一个项目时,它没有在UI中反映出来......出了什么问题......?

<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;

1 个答案:

答案 0 :(得分:5)

如果列表发生变化,ObservableCollection将通知GUI。但是,您正在使用AllDivisions = existingPlaceList行更改整个列表本身。你必须为包含AllDivisions属性的类实现INotifyPropertyChanged,以便在换出列表时告诉GUI。