Custom MarkupExtension to set parameters of ViewModel

Jan 13, 2012 at 11:11 PM

Now ViewModel parameter whether it is shared is set by choosing right AttachedProperty of three ViewModel, SharedViewModel, NonSharedViewModel.

Because of limitation, that parameters cannot be set the contract type of [ExportViewModel] is object.
So in this case if I want to import this ViewModel somwhere i need to write

[ImportingConstructor]
public LoginRegistrationViewModel(
[Import("LoginViewModel", typeof(object))]LoginViewModel loginViewModel,
[Import("UserRegistrationViewModel", typeof(object))]UserRegistrationViewModel userRegistrationViewModel)

So, if there will be a custom MarkupExtension we can use it this way:

mvm:ViewModelLocator.ViewModel="{ViewModelBinding ContractName=ShellViewModel}"
mvm:ViewModelLocator.ViewModel="{ViewModelBinding ContractName=ShellViewModel, CreationPolicy=Shared}"
mvm:ViewModelLocator.ViewModel="{ViewModelBinding ContractType={x:Type vm:ShellViewModel}}"
mvm:ViewModelLocator.ViewModel="{ViewModelBinding ContractName=ShellViewModel, ContractType={x:Type vm:ShellViewModel}}"


And then even this way

[ImportingConstructor]
public LoginRegistrationViewModel(LoginViewModel loginViewModel, UserRegistrationViewModel userRegistrationViewModel)

It will be supported in Silverlight 5 too.

I've already successfully ported x:Static and x:Type Markup Extensions.