Using ContentControl to display views in WPF doesn't work

I am currently working on a small project to learn WPF better and the question I was asking myself is how to work with different views if I want to follow the MVVM pattern.

I came across this question while answering my question , but unfortunately it doesn't work. Instead of displaying my view, only the namespace of the corresponding view model is displayed. For example, instead of seeing my LoginView, I see a white screen that says "JollyFinance.ViewModels.LoginViewModel".

Here is my MainWindow.xaml code:

<Window.Resources>
    <!-- Different pages -->
     <DataTemplate x:Key="LoginView" DataType="vm:LoginViewModel">
        <views:LoginView />
    </DataTemplate>
</Window.Resources>

<Window.DataContext>
    <vm:MainViewModel/>
</Window.DataContext>

<Grid>
    <ContentControl Content="{Binding CurrentViewModel}"/>
</Grid>

      

Here is my MainViewModel.cs:

public class MainViewModel : BindableObject
{
    private ViewModelNavigationBase _currentViewModel;

    public MainViewModel()
    {
        CurrentViewModel = new LoginViewModel();
    }

    public ViewModelNavigationBase CurrentViewModel
    {
        get { return _currentViewModel; }
        set
        {
            if (_currentViewModel != value)
            {
                _currentViewModel = value;
                RaisePropertyChanged();
            }
        }
    }
}

      

My LoginViewModel.cs file:

    public LoginViewModel()
    {
        LoginCommand = new RelayCommand(Login);
        NavigateCommand = new RelayCommand(Login);
    }

    private void Login(object param)
    {
        Popup codePopup = new Popup();
        TextBlock popupText = new TextBlock();
        popupText.Text = "Test";
        popupText.Background = Brushes.LightBlue;
        popupText.Foreground = Brushes.Blue;
        codePopup.Child = popupText;
    }

    public String Username { get; set; }

    public String Password { get; set; }

    public ICommand LoginCommand { get; set; }
}

      

My LoginView.xaml file is UserControl

composed of several Buttons

and TextBlocks

. BindableObject

is just an abstract class that I use to implement the interface INotifyProperty

. And ViewModelNavigationBase

- this is a class that will inherit all of my view models; it contains nothing at the moment.

How can I solve this so that it displays my view instead of the string representation of the associated view model?

+3


source to share


2 answers


I finally found what the problem is. For some reason, when you declare x: Key on the DataTemplate, this created the ContentControl Content as a string. I removed the x: DataTemplate key and now everything works. I also had to apply nemesv's answer for it to work.

Before:

<DataTemplate x:Key="LoginView" DataType="vm:LoginViewModel">

      



After (resolves the problem):

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

      

+7


source


WPF does not support implicit DataTemplates string

as type identifier.

You need to use x:Type

Markup Extension
to specify the model type of your view:



<DataTemplate DataType="{x:Type vm:LoginViewModel}">
    <views:LoginView />
</DataTemplate>

      

+6


source







All Articles