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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:15:39+00:00 2026-05-17T22:15:39+00:00

In MainPage.xaml.cs (Silverlight Application) I can do something like this: StackPanel myStackPanel = new

  • 0

In MainPage.xaml.cs (Silverlight Application) I can do something like this:

StackPanel myStackPanel = new StackPanel();

Button myButton = new Button();
myButton.Content = "Button";
myButton.Width = 200;
myButton.Height = 30;

Button myButton1 = new Button();
myButton1.Content = "Button 1";
myButton1.Width = 200;
myButton1.Height = 30;

myStackPanel.Children.Add(myButton);
myStackPanel.Children.Add(myButton1);

this.LayoutRoot.Children.Add(myStackPanel);

What is the equivalent of this code in a custom control when I’m trying to create these controls from the code?

Update:

My question is probably too confusing. I’l try better formulation.
So, I have

Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DemoAddControlLib">

    <Style TargetType="local:DemoControlShowtime">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:DemoControlShowtime">
                    <Grid x:Name="LayoutRootControl">
                        <Button x:Name="Button1" Content="Hi" Width="150" Height="30"></Button>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

And code:

DemoControlShowtime.cs

[TemplatePart(Name = "Button1", Type=typeof(Button))]
public class DemoControlShowtime : Control
{
    public DemoControlShowtime()
    {
        this.DefaultStyleKey = typeof(DemoControlShowtime);
    }

    // Events
    public override void OnApplyTemplate()
    {
        Button1 = (Button)GetTemplateChild("Button1");
    }

    private Button button1;

    private Button Button1
    {
        get { return button1; }
        set
        {
            if (button1 != null)
            {
                Button1.Click -= new RoutedEventHandler(myButton_Click);
            }

            button1 = value;

            button1.Click += new RoutedEventHandler(myButton_Click);
        }
    }

    void myButton_Click(object sender, RoutedEventArgs e)
    {
        Button1.Content = "Hello Button";


    }
}

If I click on Button1 the Content changes from “Hi” to “Hello Button”. I want, when Button1 is clicked, to add StackPanel with two buttons as its Children into the Grid LayoutRootControl.
I know there is Visibility property and put it into the xaml would be easier but I’m curious how to do it from the code.

I hope this is much clearer than the question was before.

  • 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-17T22:15:40+00:00Added an answer on May 17, 2026 at 10:15 pm

    The code isn’t really any different to what you have. The only variation is that the field LayoutRoot is not created for you.

    However with this line of code:-

     Grid LayoutRoot = GetTemplateChild("LayoutRootControl") as Grid;
    

    The rest of your code would be identical (although you should test whether LayoutRoot is null first).

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

Sidebar

Related Questions

I have a Silverlight app with two XAML Pages - MainPage and OtherPage How
I have a Page in Silverlight which is Navigated from the MainPage.xaml, called OpenPage.xaml,
When I create a new Silverlight Application (C# if that matters) using Silverlight 4
I have an app built with the Silverlight Navigation Application Template. I have a
I'm trying to write a silverlight application that takes in InitParams and then uses
When I try to run a simple, basic Silverlight application, I got the following
I am trying to build a library of silverlight controls where the client can
Can anybody tell me why is this not working. I have created a WCF
FYI I'm pretty new to Silverlight. Okay, so I want to build a simple
I have a Silverlight user control that has an Image control within it. This

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.