DataTemplate ViewModel with non-empty constructor?

How can I datatemplate a UserControl with a ViewModel with a NON-Empty constructor?

public PersonViewModel(Person person)
{
   _person= person;
    // do some stuff                          
}

Linking this in Xaml will fail because Ctor is not empty. But since I am using parent / child relationships with ViewModels, I have to pass the person object to the ViewModel constructor ...

How do you deal with this situation?

+5
source share
1 answer
 var person = new Person();
 var viewModel = new PersonViewModel(person);

 var view = new EditPersonView(viewModel); // use overloaded constructor to inject DataContext
 // OR
 var view = new EditPersonView{ DataContext = viewModel };

XAML, public Person Person . Person , . , , Person XAML ...

+1

All Articles