none
Dynamically Loading User Control

    Pregunta

  • Hi all

    I have a question on how to dynamically load a User Control in WPF.
    I've create a window - Main.xaml. And a user control - pushButton.xaml

    If I add pushButton.xaml throug the xaml file it works fine.

    But how do I add x number of the user control dynamically throug the .cs code?

    /Rasmus
    jueves, 17 de enero de 2008 19:04

Respuestas

  •  

    Sure.

     

    Let's say I have a StackPanel named, spnlLayoutRoot on my Window.  I want to add a new instance of a CustomUserControl.vb to the StackPanel.

     

    Dim uc as New CustomUserControl

     

    spnlLayoutRoot.Children.Add(uc)

     

    There is an awesome book available, "Applications = Code + Markup: A Guide to the Microsoft® Windows® Presentation Foundation"

     

    http://www.microsoft.com/MSPress/books/6476.aspx

     

    Great example C# code for doing exaclty what you need.

     

    viernes, 18 de enero de 2008 15:07

Todas las respuestas

  •  

    Rasmus,

     

    I do this in VB.NET everyday, I don't have the C# code.

     

    However, it is very simple.

     

    First create an instance of the UserControl you want to use.  If it is in a referenced assembly, just create an object of that Type.  If the UserControl is in another assembly, just use reflection to create an instance of the UserControl.

     

    Next, add the user control to the XAML form.  You can add the UserControl as a child of an items container or make it the Content of a control.

     

    For example, a TabItem control has a Content property.  You can assign the new instance of your UserControl as the Content of the TabItem.

     

    Depending on your application, you can add the UserControl to a StackPanel, Grid or Border Controls, Window or Page or to a ListBox, etc.

     

    Hope this helps,

     

     

     

    • Propuesto como respuesta kalyansagi lunes, 05 de enero de 2009 21:46
    jueves, 17 de enero de 2008 19:38
  • Thx. But do you by any chance have a vb example? Just so I can see some code on how you're doing it?
    Then it might be easier for me to translate than into C#.

    /Rasmus
    viernes, 18 de enero de 2008 14:04
  •  

    Sure.

     

    Let's say I have a StackPanel named, spnlLayoutRoot on my Window.  I want to add a new instance of a CustomUserControl.vb to the StackPanel.

     

    Dim uc as New CustomUserControl

     

    spnlLayoutRoot.Children.Add(uc)

     

    There is an awesome book available, "Applications = Code + Markup: A Guide to the Microsoft® Windows® Presentation Foundation"

     

    http://www.microsoft.com/MSPress/books/6476.aspx

     

    Great example C# code for doing exaclty what you need.

     

    viernes, 18 de enero de 2008 15:07
  • I couldnt get it to work.

     

    Code Snippet

    <ScrollViewer Margin="0,3.989,2.5,0" Grid.RowSpan="5" Grid.ColumnSpan="2" Grid.Row="1">

    <Viewbox x:Name="ModelViewbox" Stretch="UniformToFill" OpacityMask="#FFFFFFFF" Width="Auto" Height="Auto">

    <model:Mod3AnxCFR05 x:Name="model" Height="446.387" Width="437.437" />

    </Viewbox>

    </ScrollViewer>

     

     

    By default its like this, but i want to swap Mod3AnxCFR05 by another user control.

    The ScrollViewer is inside a Grid, but i just want to change the user control thats inside the viewbox.

     

    How can i do it?

    miércoles, 23 de abril de 2008 16:47
  • Hi Marcos.

    You can load your component at runtime with the Application.LoadComponent - pass it the URI of your xaml file, and off you go. Here's an example from a page I have that loads content dynamically from disk.

     

    Code Snippet

    private void Window_Loaded(object sender, RoutedEventArgs e) {

    var p = (Page)System.Windows.Application.LoadComponent(new Uri(

    "WpfControlLibrary1;component\\Page1.xaml", System.UriKind.RelativeOrAbsolute));

    this.pageHolder.Content = p;

    }

     

     

    Your Viewbox is a ContentControl , therefore it has a single element - so in the above snipped, you'd need to set the view box's content to the loaded control (in your case , your user control) like so :

     

    ModelViewbox.Content = yourLoadedControl;

     

    make sure your control is in the same folder as your application but it's pretty easy to diagnose missing files Wink

     

    Hope that helps a bit! Happy WPF'ing!

     

    Cheers,

    Rob,

     

    http://rob.runtothehills.org

     

    --

    * If a post answers your question, please "select Mark as Answer" so that others will know this thread contains useful info.  Thanks!

    miércoles, 23 de abril de 2008 19:12
  • Thanks Rob

     

    I just have a problem... my ModelViewBox seems to not have the Content property...Sad

    Why?

     

    Edit:

    Im using the child property.


    this.ViewBox.Child = new UserControl1();

     

    jueves, 24 de abril de 2008 14:17
  • Sorry about that Marco, it is the Child property that you should be setting. Viewbox is a control decorator that takes a single child element. if you add more than one element you'll probably get an argument exception.

     

    hehe, I can never remember the names of the content properties! Too much to remember! Sad

     

    Hope that helps,

    --

    Rob

     

     

     

    jueves, 24 de abril de 2008 18:02
  • Thanks a lot it is of good help.
    lunes, 05 de enero de 2009 21:46