2010-11-15 68 views
2

我有一個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; 
+0

請在xaml中顯示綁定並添加一個元素。 – Femaref 2010-11-15 00:19:29

+1

你爲什麼要把所有的地方都複製到一個臨時列表中?只需將該地點添加到AllPlaces即可。 AllPlaces.Add(newPlace)。剩下的就不需要了。並在構造函數中初始化AllPlaces變量。 – 2010-11-15 00:46:36

回答

5

如果列表更改,ObservableCollection將通知GUI。但是,您正在使用AllDivisions = existingPlaceList行來更改整個列表本身。您必須爲包含AllDivisions屬性的類實現INotifyPropertyChanged,才能在換出列表時告訴GUI。

+1

絕對正確。他應該增加現有的可觀察集合。更改參考不起作用。 – Aliostad 2010-11-15 00:47:44

+0

我只是想添加新的項目列表。如果我只使用這一行=> AllPlaces.Add(newPlace); ..它會工作嗎? – Relativity 2010-11-15 00:48:19

+0

@Anish:刪除AllPlaces = existingPlaceList賦值並使用AllPlaces作爲公共屬性,並返回existingPlaceList,同時使現有PlaceList成爲類實例變量,如果必須清空它,請使用Clear(),而不是每次創建新列表。 – BrokenGlass 2010-11-15 00:50:00

相關問題