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 553111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:34:16+00:00 2026-05-13T11:34:16+00:00

In my scenario I have a MainView + MainViewModel, UserControl1 + UserControl 2. In

  • 0

In my scenario I have a MainView + MainViewModel, UserControl1 + UserControl 2.
In the MainView I have 2 buttons labeled: Button_ShowUserControl1 + Button_ShowUserControl2.
At the lower part of the MainView I have a “ContentGrid” which takes/should_take… every UserControl.

My goal:

When Button_ShowUserControl1 is clicked UserControl1 is Visible and UserControl2 OR any other UserControl must be set to Collapsed. Same is valid for Button_ShowUserControl2.

My problem:

1.) As the UserControls shall be loaded at application start how can I put them all together in the one “ContentGrid”? Thats actually not possible… so how can I make one UserControl visible while the other is in the same place/”ContentGrid” just collapsed ?

2.) As 1.) seems not possible how can I instantiate all UserControls at start of the application and make them only Visible/Collapsed when respective Button is clicked?

3.) As a UserControl has a property Visibility = Visible/Hidden/Collapsed, how can I bind to a property in a ViewModel return such a value like Collapsed? I only could get a boolean value like Visibility = false/true ?

My testcode:

<Grid x:Name="LayoutRoot" Background="#FFBDF5BD" ShowGridLines="False">
    <Grid.RowDefinitions>
        <RowDefinition Height="96*" />
        <RowDefinition Height="289*" />
    </Grid.RowDefinitions>      
    <Grid HorizontalAlignment="Stretch" Name="MenuGrid" VerticalAlignment="Stretch" Background="#FFCECEFF">
        <StackPanel Name="stackPanel1" Background="#FFEDFF00" Orientation="Horizontal">
            <Button Content="User Data 1" Height="35" Name="button1" Command="{Binding  Path=ShowUserControl1Command}" Width="150" Margin="100,0,0,0" />
            <Button Content="User Data 2" Height="35" Name="button2" Width="150" Margin="100,0,0,0" />
        </StackPanel>
    </Grid>
    <Grid Grid.Row="1" HorizontalAlignment="Stretch" Name="ContentGrid" VerticalAlignment="Stretch" Background="#FFB15454" />
</Grid>

<UserControl x:Class="SwapUserControls.MVVM.UserControl2"
         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" 
         xmlns:vm="clr-namespace:SwapUserControls.MVVM.ViewModel"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" Visibility="{Binding IsUserControl1Collapsed, Path=Value}">

<UserControl.Resources>
    <vm:MainViewModel x:Key="MainViewModelID" />
</UserControl.Resources>

<UserControl.DataContext>
    <Binding Source="{StaticResource MainViewModelID}" />
</UserControl.DataContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="228*" />
        <RowDefinition Height="72*" />
    </Grid.RowDefinitions>
    <Button Content="UserControl2" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="112,27,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
    <DataGrid HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Stretch" Background="#FFC046F8" />
</Grid>

public class MainViewModel : ViewModelBase
{
    RelayCommand _ShowUserControl1Command;
    private bool _IsUserControl1Collapsed;

    public RelayCommand ShowUserControl1Command
    {
        get
        {
            if (_ShowUserControl1Command == null)
            {
                _ShowUserControl1Command = new RelayCommand( () => ShowUserControl1() );                       
            }
            return _ShowUserControl1Command;
        }
    }

    public void ShowUserControl1()
    {
        _IsUserControl1Collapsed = true;
    }

    public bool IsUserControl1Collapsed 
    {          
        get
        {
            return _IsUserControl1Collapsed;
        }  
    }        
}

Yes the code is wrong, therefore I ask here 🙂

  • 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-05-13T11:34:16+00:00Added an answer on May 13, 2026 at 11:34 am

    You only have 2 things wrong with this code.

    1) You can’t set the visibility of a usercontrol directly… you have to set it on a container:

    <Grid Visibility="Collapsed">
        <myControls:MyUserControl />
    </Grid>
    

    2) Visibility is not a boolean value, it is an enum. As such, you will need to use a converter to convert from boolean to Visibility. Observe:

    <Window ...>
    <Window.Resources>
         <BooleanToVisibilityConverter x:Key="BoolToVis" />
    </Window.Resources>
    
    <Grid Visibility="{Binding ShouldShowUsercontrol1, Converter={StaticResource BoolToVis}}">
         <myControls:MyUserControl />
    </Grid>
    </Window>
    

    That should be it. Hope this helps.

    There are other things that you are leaving clues about that might affect the ability of this to work. For example, you don’t show the biggest container element… are you wrapping everything in a StackPanel? If you are wrapping everything in a Grid, for example, the controls will overlay everything in layers.

    Try these changes I suggest… it should get you closer.


    Edit: Another idea using data templates

    Another thing you could do is make sure you have a unique ViewModel for each of these views you want to show and hide:

    public class MyFirstViewModel : ViewModel
    {
    
    }
    
    public class MySecondViewModel : ViewModel
    {
    
    }
    

    Then from your “parent” or “main” ViewModel, you show or hide the views you want by virtue of having them in a collection of ViewModels:

    public MyMainViewModel : ViewModel
    {
         public ObservableCollection<ViewModel> ViewsToShow
         {
              ...
         }
    
         public void ShowFirstViewModel()
         {
              ViewsToShow.Add(new MyFirstViewModel());
         }
    }
    

    To wire everything up in your view, you would then datatemplate these types with their user controls (but this would not cause those views to be instantiated unless they were needed:

    <Window ...>
         <Window.Resources>
              <DataTemplate DataType="{x:Type myViewModels:MyFirstViewModel}">
                   <myViews:MyFirstView />
              </DataTemplate>
    
              <DataTemplate DataType="{x:Type myViewModels:MySecondViewModel}">
                   <myViews:MySecondView />
              </DataTemplate>
         </Window.Resources>
    
         <ItemsControl ItemsSource="{Binding ViewsToShow}" />
    
    </Window>
    

    And for any ViewModels you put in “ViewsToShow”, the view will automatically see that and template in the appropriate view. Again, without instantiating it before it’s needed.

    This is probably a little cleaner than putting everything single thing in the View and setting visibility, but it would be dependent on you have a unique view model type for every view, which might not be the case.


    The question of saving state comes up when using the DataTemplated approach. The solution here is to tread your ViewModel as the state of the control and design both your ViewModels and your Views accordingly. Here is an example that allows you to swap out your Views using DataTemplating, but switching back and forth saves state.

    Assume you have the setup from the last section w/ 2 viewmodels that have datatemplates defined. Let’s change up the MainViewModel a little:

    public MyMainViewModel : ViewModel
    {
         public RelayCommand SwapViewsCommand
         {
              ...
         }
    
         public ViewModel View
         {
              ...
         }
         private ViewModel _hiddenView;
         public MyMainViewModel()
         {
              View = new MyFirstViewModel();
              _hiddenView = new MySecondViewModel();
              SwapViewsCommand = new RelayCommand(SwapViewModels);
         }
    
         public void SwapViewModels()
         {
              var hidden = _hiddenView;
              _hiddenView = View;
              View = hidden;
         }
    }
    

    And a few changes to the main view. I’ve omitted the DataTemplates for brevity.

    <Window ...>
         <!-- DataTemplates Here -->
         <Button Command="{Binding SwapViewsCommand}">Swap!</Button>
         <ContentControl Content="{Binding View}" />
    </Window>
    

    That’s it. The secret here is I’m saving the reference to the original view model. This way, let’s say there is a string property in a viewmodel and an associated textbox in the DataTemplated usercontrol with a two-way binding then the state will essentially be saved.

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

Sidebar

Related Questions

Scenario: I have a Question which has 2 radio buttons & two subforms: subformA
Scenario I have a DevExpress DataGrid which is bound to a DataSet in C#.
Scenario: I have an administration-application which manages the user accounts for another application. Now
Scenario: We have MVC 4.0 web application with display myOrders which returns json result
Scenario: I have an application that has a config table which stores the config
Scenario I have an web application which needs some calculations and processing on data.
Scenario: I have a WPF desktop application which will be distributed on different machines
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Scenario: I have a console application that needs to access a network share with
Scenario: I have an outline where I have links for my menu. Each link

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.