0

In my MVVM application, I am trying to create a reusable user control that's been shared between multiple applications, here's the constructor

public MyUserControl(IMyViewModel viewModel)
{
    InitializeComponent();
    DataContext = viewModel;
}

So my plan is that each application will just feed it's own viewmodel to the shared view.

Now the questions is how to pass viewmodel object from XAML code to the constructor:

<my:MyUserControl "somehow pass a viewmodel object from here">

Thank you

inside
  • 3,047
  • 10
  • 49
  • 75

1 Answers1

6

You can't, you need a parameterless constructor. The best you can do is bind directly:

<my:MyUserControl DataContext="{Binding Path.To.ViewModel}">

Actually, I'm not entirely right. In WPF 4, you can using an arguments directive. But I really wouldn't, it's not really idiomatic WPF.

Charles Mager
  • 25,735
  • 2
  • 35
  • 45