Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazy WPF binding

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?

like image 259
Cartesius00 Avatar asked Jun 25 '11 10:06

Cartesius00


1 Answers

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; }
    set
    {
        _isExpanded = value;
        OnPropertyChange("IsExpanded");
        OnPropertyChange("Content");
    }
}

public SomeType Content
{
    get
    {
        if (!_isExpanded)
            return null;
        return LoadContent();
    }
}
like image 197
Thomas Levesque Avatar answered Sep 21 '22 22:09

Thomas Levesque