I have a WPF application in PRISM architecture.
I have a user-control (view) that has 2 user-controls inside it.
The reason why the ‘Search user-control’ is in its own user-control, is because I plan on reusing it in my application.
The main user-control (black) is called ‘MainView’ and has 2 user-controls in it:
– A user-control that has several textbox control I can filter by, and a ‘Search’ button
– A user-control that has a grid where I display the results
Each of these user-controls (and the parent, “Main View”) are ‘Views’ and have ‘View-Models’.
Everything is shown fine, except for the fact that the ‘child’ user-controls do not create their own ‘View-Models’ automatically.
In the view-model’s classes I have the ‘Export’ attribute,
and in the View’s code-behind I have this snippet:
[Import]
FilterFieldsViewModel ViewModel
{
set
{
this.DataContext = value;
}
}
Setting the ‘View-Model’ does happen for the ‘Main View’, but not for the child views…
I have a command binded to the ‘Search’ button of the ‘Search User-control’,
and when the application loads – I get an error in the output window, saying :
BindingExpression path error: ‘SearchCommand’ property not found on ‘object’ ”MainWindowViewModel’ (HashCode=22047425)’. BindingExpression:Path=SearchCommand; DataItem=’MainWindowViewModel’ (HashCode=22047425); target element is ‘Button’ (Name=’Search’); target property is ‘Command’ (type ‘ICommand’)
For some reason the command from the ‘child’ user-control tries to bind to the view-model in the main window’s view model, because for some reason – the child user-control’s view model is not instantiated.
Why is this ?

Your dependency injection container (MEF) is not registering imports on the sub-views, because they are created by the
MainViewand not by MEF. What you probably want to do is create a property on yourMainViewModel:Then in XAML:
This enables MEF to create an instance of
FilterFieldsViewModelfor you and sends it down to theSearchUserControl.This is the basic concept of course, maybe you would want to create a ‘
SearchViewModelBase‘ or something that has theFilterFieldsViewModel.