Where to create parametrized ViewModel? Where to create parametrized ViewModel? wpf wpf

Where to create parametrized ViewModel?


Use an ObjectDataProvider if you want to specify constructor parameters:

<Window.DataContext>    <ObjectDataProvider ObjectType="vm:MyViewModel"        xmlns:sys="clr-namespace:System;assembly=mscorlib">        <ObjectDataProvider.ConstructorParameters>            <sys:String>A string parameter</sys:String>            <sys:Int32>42</sys:Int32>        </ObjectDataProvider.ConstructorParameters>    </ObjectDataProvider></Window.DataContext>


I have no idea how to pass a contructor-parameter, I think it can't be done (but it would be nice if someone proved me wrong).

What you can do is set properties on your ViewModel, as in

<Window.DataContext>    <vm:MyViewModel  MyProperty="Hello" /></Window.DataContext>