سؤال

My main view model (mainViewModel) contains an inner view model (innerViewModel) that is bound to it's view (innerView) using a DataTemplate.

In my mainView I have a ContentPresenter that has it's Content property bound to the innerViewModel and it's Visibility bound to an IsVisible property. The Visibility property is Collapsed and will very rarely be set to Visible.

My mainViewModel is one of ~1000 in a list. Currently each instance of mainViewModel also has an instance of innerViewModel that is almost never used. Is there a standard way of lazily instantiating the innerViewModel such that it is created only when it's view becomes visible? The normal approach of lazily instantiating does not work, since innerViewModel is used in a binding.

Perhaps there is a better approach to housing a rarely seen control within another one?

هل كانت مفيدة؟

المحلول 2

"Is there a standard way of lazily instantiating the innerViewModel such that it is created only when it's view becomes visible?"

The solution is to do just that. Now I don't actually create an instance of innerViewModel until the IsVisible property is first set to true. This means that the Content of the ContentPresenter on the mainView is bound to null for most of the time, but this does not seem to be a problem.

نصائح أخرى

Try using the Lazy object for lazy initialization.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top