Where to create parametrized ViewModel?

前端 未结 2 1718
别跟我提以往
别跟我提以往 2020-12-29 12:51

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


    
         


        
相关标签:
2条回答
  • 2020-12-29 13:10

    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>
    
    0 讨论(0)
  • 2020-12-29 13:33

    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>
    
    0 讨论(0)
提交回复
热议问题