views:

326

answers:

2

Hey, I was wondering if there was a way of calling a method or updating a property on my ViewModel object when WPF binds to the object ?

The reason I want to do this is that when my viewModel objects get created their data model only contains an ID that is used to query data from the database when necessary. So when the user navigates to that object I want the view to notify the ViewModel object that its being watched and as a result tell the data model to update its values from the DB and put my ViewModel object into a loading state

If the ViewModel objects knew to update them selves when they were displayed on screen I could avoid having to manually refresh all the objects.

Thanks!

A: 

You could add a Selected property to your ViewModel that gets set when the object becomes selected. When Selected turns to true, you could hit up your database.

Jake Pearson
I guess I should have been more clear.While that does work if I have a single object, my objects are referencing other objects that reference other data, etc.So I could iterate through all the objects manually and call a refresh on them it seems kinda painful.Any other tips ?
HaxElit
In that case, I don't have any really bright ideas, sorry :(
Jake Pearson
+2  A: 

When WPF binds to the object in your ViewModel, it's going to use the properties getter to fetch the value.

It sounds like you're trying to use lazy evaluation - just make the getter lazily instantiate the information from the DB:

private int entityId; // Set in advance
private Entity entityToFetch; // Will be fetched lazily

public Entity EntityToFetch
{
    get 
    {
        if (this.entityToFetch == null) // || this.entityToFetch.Id != this.entityId) - add this if you're letting this change at runtime...
        {
            this.entityToFetch = DataAccessLayer.FetchEntityForId(this.entityId);
        }

        return this.entityToFetch;
    }
}
Reed Copsey
This seems like a good way to go - you could chain getters on your objects to (lazily) instantiate as each instance gets hit for the first time..
andyp
I'll give it a go and see how it works. The only thing that scares me about this is when you are debugging it auto loads the objects as you are stepping through code so it makes it really hard to know what got loaded when.
HaxElit
Ash
@Ash: You can set this up using a Lazy<T> (built-in in .NET 4, but easy to create in .NET 3.5) that just does the loading for you.... It can be completely contained within the storage provider that way, provided you have a (thin) layer over the top of your storage.
Reed Copsey
Well I bit the bullet and did it... It worked out better then I thought. I made it return a "Loading" string for all the properties if my object wasn't fully loaded. Actually works out kinda nice.Btw if you don't want this to load the properties while debugging looking into the [DebuggingProxy] attribute. Yout can make a proxy class that returns the direct values with out calling the loading methods.
HaxElit