Take the 2-minute tour ×

I have Expander from WPF (and using Entity Framework 4 and MVVM pattern) which contains ContentControl bound to some inner ViewModel. All I want is to bind this content control LAZILY. That is I want my ViewModel to be "get" when the Expander is opened.

How to do that? How to make complex windows with inner ViewModels faster?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

You could add an IsExpanded property to your ViewModel, bind the expander to it, and take the value of that property into account when returning the content of the ContentControl:

private bool _isExpanded;
public bool IsExpanded
    get { return _isExpanded; }
        _isExpanded = value;

public SomeType Content
        if (!_isExpanded)
            return null;
        return LoadContent();
share|improve this answer
I'd cache the content! This way might be calling the LoadContent method many times. –  Erno de Weerd Jun 25 '11 at 21:04
@Erno, sure, you could do that, but it depends on what the OP wants exactly... –  Thomas Levesque Jun 25 '11 at 23:18
I added the comment because most of the time you'll want lazy loading because getting the resources is expensive. It wasn't meant as a critique. Just an addition. –  Erno de Weerd Jun 26 '11 at 6:49

Another option similar to the above might be to create an ObservableCollection, but only populate it on the first time the expander is opened.

share|improve this answer

Your Answer


