title: adding User control / Page to windows xaml programmatically in WPF
body: <p>I want to switch between my two user controls AddUsers.xaml (user control) and Viewusers.xaml(user Control) programmatically in Window1.xaml (main Window). </p>
  
  <p>I'm trying to switch user controls by Button Event in Window1.xaml.</p>
  
  <p>my Window1. xaml goes like this </p>
  
  <pre><code> &lt;Window x:Class="SwitchUsers.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="500" Width="525"&gt;
  &lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="100" /&gt;
          &lt;RowDefinition Height="400*" /&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;StackPanel Grid.Row="0" &gt;
      &lt;Button Content="Swithc User Control " Height="23" HorizontalAlignment="Right"     Margin="0,40,284,0" Name="btnSwittch" VerticalAlignment="Center" Width="168" /&gt;
      &lt;/StackPanel&gt;
  
      &lt;StackPanel Grid.Row="1" &gt;
         &lt;!--Here I want to display  two user controls by switching from button on Top --&gt;            
      &lt;/StackPanel&gt;
  &lt;/Grid&gt; 
  </code></pre>
  
  <p>I have two User Controls addUser.xaml  and viewUser.xaml</p>
  
  <p>addUser. xaml  code:</p>
  
  <pre><code> &lt;UserControl x:Class="SwitchUsers.addUser"
           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" Loaded="UserControl_Loaded"&gt;
       &lt;Grid&gt;
      &lt;TextBlock Height="23" HorizontalAlignment="Left" Margin="29,79,0,0"  Name="textBlock1" Text=" Enter Your Name" VerticalAlignment="Top" /&gt;
      &lt;TextBlock Height="23" HorizontalAlignment="Left" Margin="29,105,0,0" Name="textBlock2" Text="Enter Your Password" VerticalAlignment="Top" /&gt;
      &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="144,76,0,0" Name="txtBxName" VerticalAlignment="Top" Width="120" /&gt;
      &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="144,105,0,0" Name="txtBxPassword" VerticalAlignment="Top" Width="120" /&gt;
      &lt;Button Content="Log In" Height="23" HorizontalAlignment="Left" Margin="171,160,0,0" Name="btnLogin" VerticalAlignment="Top" Width="93" /&gt;
      &lt;/Grid&gt;
      &lt;/UserControl&gt;
  </code></pre>
  
  <p>and my second user control  viewUser.xaml</p>
  
  <pre><code>  &lt;UserControl x:Class="SwitchUsers.viewUser"
           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"&gt;
     &lt;Grid&gt;
      &lt;!--    I hidden  all UI Controls here to keep code short --&gt;    
     &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>