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

  • SEARCH
  • Home
  • 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 8399383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:17:26+00:00 2026-06-09T21:17:26+00:00

I have a master view and two subviews. I would like to switch from

  • 0

I have a master view and two subviews. I would like to switch from SubViewA to SubViewB when clicking on the button on SubViewA. The masterview contains a contentpresenter which is binded to View, and initialized to SubViewB when loaded. When clicking on the button on SubViewA the SubViewB constructor is called, but the control is never loaded. What am I missing? I’ve also tried by just setting the contenttemplate:

<ContentPresenter x:Name="contentPresenter" Content="{Binding View, PresentationTraceSources.TraceLevel=High}" />

which does not work either.

MainWindow:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:local="clr-namespace:WpfApplication2">

<Grid>

    <TextBlock Text="MasterViewPage" />

    <ContentControl x:Name="content" Content="{Binding View}">
        <ContentControl.Resources>
            <DataTemplate DataType="{x:Type local:SubViewModelA}">
                <local:SubViewA></local:SubViewA>
            </DataTemplate>
            <DataTemplate DataType="{x:Type local:SubViewModelB}">
                <local:SubViewB></local:SubViewB>
            </DataTemplate>
        </ContentControl.Resources>
    </ContentControl>

</Grid>
</Window>

public partial class MainWindow
{
    public MainWindow()
    {
        Loaded += MainWindow_Loaded;
        InitializeComponent();
    }

    private void MainWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        DataContext = new MainViewModel();
    }      
}

SubViewA:

<UserControl x:Class="WpfApplication2.SubViewA"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

<Grid Margin="0,40,0,0">
    <TextBlock Text="Subview A" />

    <Button Height="50" Width="120" Content="Open View B" Command="{Binding OpenViewCommand}" />
</Grid>
</UserControl>

public partial class SubViewA
{
    public SubViewA()
    {
        Loaded += SubViewA_Loaded;
        InitializeComponent();
    }

    private void SubViewA_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        DataContext = new SubViewModelA();
    }
}

ViewModels:

public class MainViewModel : NotifyPropertyChanged
{
    private object _view;

    public object View
    {
        get { return _view; }
        set
        {
            _view = value;
            RaisePropertyChanged(() => View);
        }
    }

    public MainViewModel()
    {
        View = new SubViewA();
    }
}

public class SubViewModelA : MainViewModel
{
    public ICommand OpenViewCommand
    {
        get { return new DelegatingCommand(OpenView); }
    }

    private void OpenView()
    {
        View = new SubViewB();
    }
}

public class SubViewModelB : MainViewModel
{
}

Thanks in advance.

  • 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-09T21:17:28+00:00Added an answer on June 9, 2026 at 9:17 pm

    OK, the solution that worked for me was:

    MainView.xaml:

        <ContentControl x:Name="content">
            <ContentControl.Style>
                <Style TargetType="{x:Type ContentControl}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding View, PresentationTraceSources.TraceLevel=High}" Value="SubViewA">
                                <Setter Property="ContentTemplate">
                                <Setter.Value>
                                        <DataTemplate>
                                            <local:SubViewA />
                                        </DataTemplate>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding View, PresentationTraceSources.TraceLevel=High}" Value="SubViewB">
                                <Setter Property="ContentTemplate">
                                <Setter.Value>
                                        <DataTemplate>
                                            <local:SubViewB />
                                        </DataTemplate>
                                    </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ContentControl.Style>
        </ContentControl>
    

    and in SubViewA.xaml:

    <Button Height="50" Width="120" Content="Open View B" Command="{Binding Path=DataContext.OpenViewCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}, PresentationTraceSources.TraceLevel=High}" />
    

    Reason was that View wasn’t set on MainViewModel, but on the subview instead (which inherited from MainViewModel). When removing the inheritance to actually set the view on MainViewModel everything worked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking to make a Master Detail View. I have two tables tbFamily
I have a UISplitViewController with the master view set up like this: UITabBarController Tab1:
I have two tables nested (master view) for representing my data. The parent table
I have a partial view ( Partial.ascx ), two master pages( Master1.Master and Master2.Master
I have a master detail Application in xCode. In Master View Controller an Array
I have a modal view controller and a master view controller. When I open
I have a tab bar controller in the master view of a split view
When using a spilt view controller, I have a Master and Detail View. When
In my iPhone Core Data app I have it configured in a master-detail view
In my view, I have included the Master page as follows: <%@ Page Title=

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.