views:

33

answers:

1

I have recently parametrized my ViewModel's contructor. Before that, I was doing this in my window:

<Window.DataContext>
    <vm:MyViewModel />
</Window.DataContext>

The framework instantiated the ViewModel for me.

I know I can set DataContext in code but I would prefer a XAML way so designer can display my test data when designing.

Is this possible?

+1  A: 

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>
Quartermeister
Thank you, I knew it could be done.
Kugel