Click here to Skip to main content
13,143,459 members (31,577 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 25 Oct 2013

Loading and Unloading UserControls

, 25 Oct 2013
Rate this:
Please Sign up or sign in to vote.
Loading and unloading ChildUserControl instances at Runtime


I'm in the process of making a SHELL application that uses Microsoft Ribbon and a single Dockpanel UserControl UIPanel. This UIPanel is just an empty shell that gets filled in on runtime.

In short, at runtime, we're going to load a UserControl from a RibbonButton. Then via a button within the UserControl, the UserControl is going to unload itself.

Using the Code

Create your WPF window: Shell.xaml and add a Ribbon and an empty DockPanel.

XAML: Shell.xaml (Window)




        Title="Shell" >

            <RowDefinition Height="Auto"/>
        It is important to put the UIPanel before that of the Ribbon
        so it is loaded before the SelectionChanged action occurs
        <DockPanel x:Name="UIPanel" Grid.Row="1" />
        <Ribbon x:Name="RibbonWin" 

               SelectedIndex="0" WindowIconVisibility="Hidden" >
          <RibbonButton  Content="AddUserControl" Click="Add_Click"/>

Tip: Put the DockPanel statement before that of the Ribbon so it is loaded before that of the Ribbon.

Next, create a UserControl that will be loaded into the UIPanel upon the click of a RibbonButton. For example, ucChild.xaml.

<UserControl x:Class="MyProject.UserControls.ucChild"





    <Button  Content="Exit" Click="Exit_Click" />

So how does it work. Well, the whole idea is to make sure that the child knows who the parent is at runtime. This we do by adding a property: ParentControl and cast it to the Type of my UIPanel DockPanel. And... via the Exit_click the unload statement.


namespace MyProject
   public DockPanel ParentControl {get; set;}
   public ucChild()

   private void Exit_Click(object sender, RoutedEventArgs e)
      if (this.ParentControl != null)

So next the loading of the UserControl..

During runtime, upon the click of the RibbonButton an instance of the UserControl is created and then it is told who its Parent is and then it is loaded into our UIPanel.


using System.Windows.Controls.Ribbon;

namespace MyProject
    public partial class Shell : RibbonWindow
        private void Add_Click(object sender, RoutedEventArgs e
            ucCild ChildWindow = new ucChild();
            ChildWindow.ParentControl = this.UIPanel;


In short, the parent control when loading its child tells it who its parent is and thus the child can unload itself.


First image is that of the Shell, the Ribbon and the RibonButton "AddUserControl":

The second Image shows the UserControl and its label and Button "Exit":

Points of Interest

I learnt that this article is too short for publishing, so the question is how long must an article be.?

In the next update, I'll try and make a small project to elaborate this article.


  • 2013-10-25. :: Initial text, no pics , no code
  • 2013-10-26. :: Pics[001,002] + Source code


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Belgium Belgium
Developer within C#, Dynamics NAV (Navision), Php environments.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170915.1 | Last Updated 26 Oct 2013
Article Copyright 2013 by kribo
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid