views:

23

answers:

2

hey guys,
I have a WPF application that contains windows with few user controls and Coordinator object. the window and all its user controls pointing to an object, which instace is in the Coordinator, by thier DataContext.
the problem is that I want to change this object (e.g. create new object()) in the Coordinator but I want all the dataContexts to point to the new object.
I tried to send the object by ref to the window constructor but it didn't help. any idea about how can I rewrite the memory location that all pointers are pointing to?
(I don't want to repalce the properties in object since its a lot of work nor to use a middle object that points to the replaced object)
Thanks
Eran

A: 

Unless I missed something, you are using datacontext in your application so when you create a new object, can you not set the datacontext to the new object and it will be bound to all of your user controls.

ClearValue(DataContextProperty);

DataContext = NewlyCreatedObject;
VoodooChild
i simplify my question in order to explain the problem actually i have a lot of windows with different datacontexts each with the above description hence i would like to change all the objects from the Coordinator and avoid changing the windows properties
Eran
+1  A: 

I tried to send the object by ref to the window constructor but it didn't help. any idea about how can I rewrite the memory location that all pointers are pointing to?

It's normal because, even if you change the object the reference is pointing to, your user controls have no way to know that something has changed : you have to notify them.

You have two solutions to do so :

  • make your Coordinator implement INotifyPropertyChanged and raise NotifyPropertyChanged when the object reference changes,

  • make your object available via a DependencyProperty and use a property wrapper. When changed the DependencyProperty will automatically notify its observers.

Serious