2
votes

So I've started looking at the Catel MVVM framework and it looks like it will solve a couple of problems I have encountered, but I have one really silly issue. I think I'm just missing something small.

I took one of my smaller WPF projects to switch over the Catel as a way for me to learn it. I have a simple 'Player Registration' form, with fields like name and surname. I recreated my original view model by using the vm codesnippet and all is good, all the properties and attributes I've set up as I've read in the documentation.

I then changed the UserControl I used for 'Player Registration' (PlayerRegistrationView) to a catel:UserControl. I placed PlayerRegistrationView on a standard WPF Window (nothing else, just a xmlns for the View and the view as the only content on the window, no attributes)

But here is my problem:

I have a MainWindow with a button on to open the Window for the player registration. The on-click event simply is this:

private void ButtonPlayerClick(object sender, RoutedEventArgs e)
    {
       var playerRegistration = new PlayerRegistrationDialog
            {
                Owner = this,
                DataContext = new PlayerRegistrationViewModel(),
            };

       playerRegistration.Show();
    }

Running my program and then clicking on the button results in an NotSupportedException on my PlayerRegistrationView: The view model of the view could not be resolved. Use either the GetViewModelType() method or IViewModelLocator

I tried making the ViewModel a static resource on the window and setting the datacontext there, but it produces the same error.

I am at a loss. What have I missed?

Thanks

1

1 Answers

2
votes

The whole point of Catel is that it automatically wires up all the views and view models. The "complex" thing that you are trying to achieve is that you have a view which is placed on a window. You want the window to have the same data context as the view in order to do some stuff in the window as well.

In Catel, it is possible to place any view with datacontext management on a DataWindow (window in Catel). Then it will work like this:

DataWindow |=> View

If the DataWindow and the View share the same view model type, then they share the same view model. For example:

  • PlayerRegistrationWindow => derives from catel:DataWindow
  • PlayerRegistrationView => derives from catel:UserControl

Since both start with PlayerRegistration, they will both be resolved to PlayerRegistrationViewModel automatically.

To show the window, the only thing you have to do is this:

var viewModel = new PlayerRegistrationViewModel();

var uiVisualizerService = ServiceLocator.Default.ResolveType<IUIVisualizerService>();
uiVisualizerService.Show(viewModel);

All will work automatically and you don't have to worry about setting any datacontext yourself.