To ensure you only get one instance of your object you could use the singleton pattern like this
public class Singleton
{
private static Singleton instance;
private Singleton() {}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
You'll notice the constructor is private so you must obtain an instance through the Instance
method. You'll also notice that method only creates an instance of the object if it doesn't exist.
You could use the same Singleton
object for all your views knowing that it'll be the same one and therefore the same data.
Alternatively, you could just declare it in a central location, you main window's viewmodel perhaps, and then everything else could access it from there.
As for updating it you could pass a reference to your object to each place that it's used and then update it directly. Or you could do something with events like this
In your view's viewmodel
public static event EventHandler MyEvent;
private void OnMyEvent()
{
if (MyEvent != null)
{
MyEvent(this, new EventArgs());
}
}
In the location where your data object is, perhaps your main window's view model
MyView.MyEvent += delegate
{
// Update your data
};
If you can't have multiple views open and/or don't want your views to respond to data changes once opened then this is probably enough. However, if you want your views to respond to data in real time you could do something with events so one view can tell another view that the data has changed and that it needs to update.