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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:48:36+00:00 2026-05-13T08:48:36+00:00

I am learning Silverlight. In the process, I’m trying to build a custom user

  • 0

I am learning Silverlight. In the process, I’m trying to build a custom user control. My ultimate goal is to be able to write the following statement in XAML:

<my:CustomControl>
  <my:CustomControl.MainControl>
    <Canvas><TextBlock Text="Hello!" /></Canvas>
  </my:CustomControl.MainContent>
</my:CustomControl>

The content of the control will be wrapped in a custom border. Once again, this is just a learning exercise. To append my border, I have create the following UserControl:

<UserControl x:Class="CustomControl"
    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">

    <Grid x:Name="LayoutRoot">
      <Border>
        <!-- CustomControl Content -->
      </Border>
    </Grid>
</UserControl>

The code-behind for this file looks like the following:

public partial class CustomControl : UserControl
{
  public UIElement MainContent
  {
    get { return (UIElement)GetValue(MainContentProperty); }
    set { SetValue(MainContentProperty, value); }
  }

  public static readonly DependencyProperty MainContentProperty =
    DependencyProperty.Register("MainContent", typeof(UIElement), typeof(CustomControl),  
    new PropertyMetadata(null));


  public CustomControl()
  {
    InitializeComponent();            
  }
}

The thing I am having a problem with is getting the MainContent to appear in my CustomControl. I am confident that I am setting it properly, but I’m not sure how to display it. I really want it to be a DependencyProperty as well so I can take advantage of data binding and animations.

How do I get the MainContent to appear in the CustomControl? Thank you

  • 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-13T08:48:36+00:00Added an answer on May 13, 2026 at 8:48 am

    First you need to wait until the rest of the control has been parsed so you need to hook the loaded event:-

    public CustomControl()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(CustomControl_Loaded);
    }
    

    Then in the loaded event assign your MainControl property to the Child property of the border. To do that its best if you give your Border an x:Name which for now I’ll simple call “border”.

    void CustomControl_Loaded(object sender, RoutedEventArgs e)
    {
        border.Child = MainControl;
    }
    

    That’ll get you going. Of course you may need to deal with the MainControl property being changed dynamically so you need add a bool isLoaded field in your control and set that in the loaded event. When true your MainControl setter should assign the incoming value to the border.Child.

    Things can start to get more complicated and in fact I don’t recommend this approach to creating a custom control. For a better approach see Creating a New Control by Creating a ControlTemplate

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

Sidebar

Related Questions

I'm learning Silverlight and am trying to ingest some new concepts. Just so I
I've just begun learning Silverlight (though I have 3 years C# experience). I'm trying
I am learning Silverlight and WPF on my own to expand my programming base.
What do I need to install to begin learning Silverlight 2? I know how
Will learning C++ help me build native applications with good speed? Will it help
Just learning Silverlight 4/RIA and i 'm stuck in a weird problem: setup an
I'm learning Silverlight 3 and, in all of my tests, when I press the
I start learning Silverlight and would like to create some simple game. I am
I have been using Silverlight for quite a bit and am learning Windows Phone
Learning WPF nowadays. Found something new today with .Net dependency properties. What they bring

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.