我有一个proprty o类型“Observable集合”...当我添加一个项目时,它不是反映在UI中的geeting ...什么是做错了...?Observable集合中的更改未反映在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;
请在xaml中显示绑定并添加一个元素。 – Femaref 2010-11-15 00:19:29
你为什么要把所有的地方都复制到一个临时列表中?只需将该地点添加到AllPlaces即可。 AllPlaces.Add(newPlace)。剩下的就不需要了。并在构造函数中初始化AllPlaces变量。 – 2010-11-15 00:46:36