Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8067389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:16:41+00:00 2026-06-05T12:16:41+00:00

I am learning WPF and MVVM at the moment (or at least I am

  • 0

I am learning WPF and MVVM at the moment (or at least I am trying to…).

I created a little sample-app, that shows a Window with 2 buttons, each of it should show a new View on Click. So I created 3 UserControls (DecisonMaker with the 2 Buttons, and one Usercontrol for each “clicktarget”).

So I bound the CotentControl of the MainWindow to a property called “CurrentView” in my MainWindowViewModel

Code of MainWindow.xaml:

<Window x:Class="WpfTestApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfTestApplication"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <local:MainWindowViewModel />
</Window.DataContext>
<Grid>
    <ContentControl Content="{Binding CurrentView, Mode=OneWay}" />
</Grid>
</Window>

Code of MainWindowViewModel:

class MainWindowViewModel
{      
    private UserControl _currentView = new DecisionMaker();
    public UserControl CurrentView
    {
        get { return _currentView; }
        set { _currentView = value; }
    }

    public ICommand MausCommand
    {
        get { return new RelayCommand(LoadMouseView); }
    }

    public ICommand TouchCommand
    {
        get { return new RelayCommand(LoadTouchView); }
    }

    private void LoadMouseView()
    {
        CurrentView = new UserControlMouse();
    }

    private void LoadTouchView()
    {
        CurrentView = new UserControlTouch();
    }
}

The initial UserControl (DecisionMaker) shows up as supposed. Also the method LoadMouseView is called. But the View doesn’t change. What am I missing?

UPDATE: Thanks so much! I missed the INotifyPropertyChanged-interface. All of your answers were just great and very accurate and helpful! I don’t know which one to accept – I think it’s the most fair way to accept the “first” answer?

I accepted blindmeis answer, as it solved the problem and helped me understand MVVM better. But every answer was really great thanks to all of you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:16:43+00:00Added an answer on June 5, 2026 at 12:16 pm

    if you wanna do mvvm – then you should have no references to your view/usercontrols in your viewmodel. you have to implement INotifyPropertyChanged! ps: if you need System.Windows namespace in your Viewmodel – then something is wrong.

    in your case what you need:

    • 1 mainviewmodel
    • 1 viewmodel for UserControlMouse
    • 1 viewmodel for UserControlTouch
    • 1 view/usercontrol for UserControlMouse
    • 1 view/usercontrol for UserControlTouch

    your mainviewmodel should have at least 2commands to switch your view and 1 property for CurrentView. in your command you simply set your CurrentView to the right viewmodel instance. at least you need two datatemplates for each viewmodel which define the right view.

    public object CurrentView
    {
        get { return _currentView; }
        set {
            _currentView = value; this.RaiseNotifyPropertyChanged("CurrentView");}
    }
    

    xaml

    <Window x:Class="WpfTestApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfTestApplication"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
     <DataTemplate DataType="{x:Type local:MyMouseViewModel}">
       <local:MyMouseUserControlView/>
      </DataTemplate>
     <DataTemplate DataType="{x:Type local:MyTouchViewModel}">
       <local:MyTouchUserControlView/>
      </DataTemplate>
    </Window.Resources>
    <Window.DataContext>
     <local:MainWindowViewModel />
    </Window.DataContext>
    <Grid>
    
     <!-- here your buttons with command binding, i'm too lazy to write this. -->
    
     <!-- you content control -->
     <ContentControl Content="{Binding CurrentView, Mode=OneWay}" />
    </Grid>
    </Window>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning WPF and MVVM and trying to stress good design. I have lots
I have recently started learning wpf and am trying to use mvvm. My understanding
I'm learning WPF and the MVVM Pattern and I'm trying to build a calendar-like
I am currently learning MVVM in WPF. I am creating an application that incorporates
As I've been learning WPF, I've been concentrating on applying only the MVVM pattern
I've recently been learning the MVVM pattern in WPF and just started making my
Sorry, I'm thrashing around a bit learning MVVM, WPF, and XAML simultaneously. I have
I just start learning WPF, is that possible I can create an IE toolbar
I'm learning WPF with MVVM and for a starter, I viewed Jason Dolinger's video
I am learning WPF and am trying to create my first UserControl. My UserControl

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.