Stefan
Stefan

Reputation: 21

WPF ContentControl does not resolve correct View from ViewModel (Catel)

I have a WPF Project with a RibbonWindow as MainWindow and I’m using Catel.

/Views/MainWindow:

public partial class MainWindow : MainWindowBase
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

This MainWindow is derived from MainWindowBase. MainWindowBase is derived from RibbonWindow and implements IDataWindow as described in the Catel documentation.

public class MainWindowBase : RibbonWindow, IDataWindow
{
    private readonly WindowLogic _logic;
    //...
}

The MainWindow is instantiated by StartupUri="/Views/MainWindow.xaml" and the correct MainWindowViewModel is resolved and instantiated.

In the MainWindow I have a content control which is bound to a Property CurrentViewModel in my MainWindowViewModel

<ContentControl DockPanel.Dock="Bottom" Content="{Binding Path=CurrentViewModel}"></ContentControl>

/ViewModels/MainWindowViewModel:

public class MainWindowViewModel : ViewModelBase
{
//..
    public static readonly PropertyData CurrentViewModelProperty = RegisterProperty("CurrentViewModel", typeof(ViewModelBase), null);

    public ViewModelBase CurrentViewModel
    {
        get { return GetValue<ViewModelBase>(CurrentViewModelProperty); }
        set { SetValue(CurrentViewModelProperty, value); }
    }
//..
}

This Property is set to a AddressViewModel which is also derived from ViewModelBase

/ViewModels/AddressViewModel:

public class AddressViewModel : ViewModelBase
{
}

I have an AddressView which is derived from Catel.Windows.Controls.UserControl: /Views/AddressView:

public partial class AddressView : Catel.Windows.Controls.UserControl
{
    public AddressView()
    {
        InitializeComponent();
    }
}

The problem is that the associated AddressView is not resolved and the content control just shows the name of the AddressViewModel.

I also have added a DataTemplate to App.xaml like below, with no effect.

<DataTemplate DataType="x:Type vm:AddressViewModel">
    <view:AddressView />
</DataTemplate>

What am I missing?

Thanks!

Upvotes: 2

Views: 920

Answers (1)

Tony
Tony

Reputation: 7445

It seems you basically forget about brackets in the expression. Should look like this:

<DataTemplate DataType="{x:Type vm:AddressViewModel}">

Upvotes: 2

Related Questions