In the code window is:

public ObservableCollection<ParkingPlace> ParkingPlaces = new ObservableCollection<ParkingPlace>(); public MainWindow() { DataContext = ParkingPlaces; ... 

in markup:

 <StackPanel Visibility="{Binding Count, Converter={StaticResource IntToVisibilityConverter}, Mode=OneWay }"> 

Converter works correctly. When adding and removing elements, everything happens as it should (the element appears / disappears).

The whole problem begins after deserialization of this collection (reassignment?)

 XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<ParkingPlace>)); using (StreamReader rd = new StreamReader("data.xml")) { ParkingPlaces = (ObservableCollection<ParkingPlace>) xs.Deserialize(rd); } 

Binding completely stops working. I also tried the NotifyPropertyChanged option, to no avail.

  • and if, after downloading from the file, again set DataContext = ParkingPlaces; ? - MaximK
  • @MaximK works! But what's the problem? I would like to mention as an answer. - ionmike

1 answer 1

For the update to work when re-assigning, you need to implement INotifyPropertyChanged for the context. Or change the DataContext each time separately:

 ParkingPlaces = (ObservableCollection<ParkingPlace>) xs.Deserialize(rd); DataContext = ParkingPlaces; 
  • Under the context means MainWindow? The option to change the DataContext is working. - ionmike
  • one
    @ionmike usually the window context is not limited to one collection. In your case, while you can not bother. - Monk
  • I read in more detail, later I will try the version with INotifyPropertyChanged, if there is no success, I will leave it as it is. Thanks for the answer! - ionmike