UserControl In WPF

The purpose of UserControls is reusability.
 
We have seen how to reuse resources in WPF & code in C#.
 
Reusing the entire screen is just insane.
 
Let's say we want to create a login form in WPF. As usual, we can go ahead and add a Window as follows,
 
UserControl In WPF
 
Code
  1. <Window x:Class="A.MainWindow"    
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
  6.         xmlns:local="clr-namespace:A"    
  7.         mc:Ignorable="d"    
  8.         Title="MainWindow" Height="120" Width="250">    
  9.     <Grid>    
  10.         <Grid.RowDefinitions>    
  11.             <RowDefinition Height="Auto"/>    
  12.             <RowDefinition Height="Auto"/>    
  13.             <RowDefinition Height="Auto"/>    
  14.         </Grid.RowDefinitions>    
  15.         <Grid.ColumnDefinitions>    
  16.             <ColumnDefinition Width="Auto"/>    
  17.             <ColumnDefinition Width="Auto"/>    
  18.         </Grid.ColumnDefinitions>    
  19.         <Label x:Name="LabelUserName"    
  20.                Content="User Name:"/>    
  21.         <Label x:Name="LabelPassword"     
  22.                Content="Password:"    
  23.                Grid.Row="1"/>    
  24.         <TextBox x:Name="TextBoxUserName"    
  25.                  Height="20"    
  26.                  Width="150"    
  27.                  Grid.Column="1"/>    
  28.         <TextBox x:Name="TextBoxPassword"    
  29.                  Height="20"    
  30.                  Width="150"    
  31.                  Grid.Column="1"    
  32.                  Grid.Row="1"/>    
  33.         <Button x:Name="ButtonLogin"    
  34.                 Height="20"    
  35.                 Width="100"    
  36.                 Content="Login"    
  37.                 HorizontalAlignment="Center"    
  38.                 Margin="20 0 0 0"    
  39.                 Grid.Row="2"    
  40.                 Grid.ColumnSpan="2"/>    
  41.     </Grid>    
  42. </Window>    
Now, what if I want to resuse this screen? We can achieve this by adding UserControl in our project.
 
UserControl In WPF
 
LoginPage.xaml (UserControl): Now cut your code from MainWindow.xaml and paste it in LoginPage.xaml.
  1. <UserControl x:Class="A.LoginPage"    
  2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
  4.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     
  5.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     
  6.              xmlns:local="clr-namespace:A"    
  7.              mc:Ignorable="d"     
  8.              d:DesignHeight="120" d:DesignWidth="250">    
  9.     <Grid>    
  10.         <Grid.RowDefinitions>    
  11.             <RowDefinition Height="Auto"/>    
  12.             <RowDefinition Height="Auto"/>    
  13.             <RowDefinition Height="Auto"/>    
  14.         </Grid.RowDefinitions>    
  15.         <Grid.ColumnDefinitions>    
  16.             <ColumnDefinition Width="Auto"/>    
  17.             <ColumnDefinition Width="Auto"/>    
  18.         </Grid.ColumnDefinitions>    
  19.         <Label x:Name="LabelUserName"    
  20.                Content="User Name:"/>    
  21.         <Label x:Name="LabelPassword"     
  22.                Content="Password:"    
  23.                Grid.Row="1"/>    
  24.         <TextBox x:Name="TextBoxUserName"    
  25.                  Height="20"    
  26.                  Width="150"    
  27.                  Grid.Column="1"/>    
  28.         <TextBox x:Name="TextBoxPassword"    
  29.                  Height="20"    
  30.                  Width="150"    
  31.                  Grid.Column="1"    
  32.                  Grid.Row="1"/>    
  33.         <Button x:Name="ButtonLogin"    
  34.                 Height="20"    
  35.                 Width="100"    
  36.                 Content="Login"    
  37.                 HorizontalAlignment="Center"    
  38.                 Margin="20 0 0 0"    
  39.                 Grid.Row="2"    
  40.                 Grid.ColumnSpan="2"/>    
  41.     </Grid>    
  42. </UserControl>   
Now update your MainWindow.xaml as follows: Just add local namespcae, from where MainWindow is supposed to load UserLogin.xaml into itself.
 
The concept is the same as the HAS-A relationship in OOPS, reusing a class into another class.  
  1. <Window x:Class="A.MainWindow"    
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
  6.         xmlns:local="clr-namespace:A"    
  7.         mc:Ignorable="d"    
  8.         Title="MainWindow" Height="120" Width="250">    
  9.     <Grid>    
  10.         <local:LoginPage></local:LoginPage>    
  11.     </Grid>    
  12. </Window>    
UserControl In WPF
 
Output screen
 
UserControl In WPF 
 
Now, there might be a question in your mind: Why do we need UserControl to serve the reusability purpose? We can directly reuse another Window. But you can't do that, as Window is the  top pest control in the visual tree, the Window cannot have another window as a child.
 
.Net will throw the following error:
 
InvalidOperationException: Window must be the root of the tree. Cannot add Window as a child of Visual.
 
That's why UserControl has been designed.
 
I hope you've come away from this with a real grasp of the UserControls and how it can be applied to your personal projects in the future.

If you've enjoyed the article, which I hope you have, please apply your knowledge to enliven the code in your projects.
 
if you have any questions or just want to connect, follow these links.