I typically solve this type of problem this way. Let's say your ViewModel is representing an object of type Dog
, and so your ViewModel is likely named DogViewModel
or DogVM
(I prefer the latter, unless I'm writing software to manage virtual machines, but I digress). So let's say this DogVM
class looks like this:
public sealed class DogVM : INotifyPropertyChanged // because bindings, yeah?
{
public string Name { /* stuff here */ }
public string Breed { /* stuff here */ }
public string Coat { /* stuff here */ }
public DateTime BornOn { /* stuff here */ }
public bool IsLoading { /* stuff here */ }
// other stuff as well, like INotifyPropertyChanged implementation
}
If your VM looks like this, what you've got is mixed concerns. You have two concerns: loading the data (and reflecting that state in a bindable way), and adapting your model to your view in a bindable way.
I'd recommend teasing this code apart into two classes. One to handle the loading state, and another to handle the display details of the Dog
class. For example:
public sealed class DogLoaderVM : INotifyPropertyChanged
{
public DogVM Dog { /* stuff here */ }
public bool IsLoading { /* stuff here */ }
// other stuff here
}
public sealed class DogVM : INotifyPropertyChanged
{
public string Name { /* stuff here */ }
public string Breed { /* stuff here */ }
public string Coat { /* stuff here */ }
public DateTime BornOn { /* stuff here */ }
// other stuff here
}
Then, for any given binding that was originally something like this:
<TextBlock Text="{Binding Name}" />
You update it to be:
<TextBlock Text="{Binding Dog.Name}" />
Using this pattern, you have a couple of options. You could leave the Dog
property in DogLoaderVM
with a value of null
until it's loaded, and then deal with the absence of values in your XAML directly (say, via TargetNullValue
).
However, if that's too complex and would require dirty, ugly, nasty things in your XAML (such things being, of course, a relative term when dealing in XAML), you could extract an interface from DogVM
, and use the null object pattern to get fine-grained control over what data gets bound to your view while the load is still pending. Some folks like to put "(loading...)" into the UI, for instance, while loads are pending and that can get pretty cumbersome and redundant in XAML, whereas it's less tedious in C#.