silverlight – How to create controls from code in a custom control? – Education Career Blog

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.

,

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).

,

It appears to me that your are just wondering how to use a custom control in multiple places.

I’ve created a custom control (MyCustomControl) that has the StackPanel shown in your code, then used it multiple times on the MainPage.

MyCustomControl.xaml

<UserControl x:Class="SilverlightApplication2.MyCustomControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<StackPanel>
    <Button Content="Button 1" Height="30" Width="200"/>
    <Button Content="Button 2" Height="30" Width="200"/>
</StackPanel>

MyCustomControl.xaml.cs

public partial class MyCustomControl : UserControl
{
    public MyCustomControl()
    {
        InitializeComponent();
    }
}

Then I’ve used that custom control twice in the main view.

MainPage.xaml

<UserControl x:Class="SilverlightApplication2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:local="clr-namespace:SilverlightApplication2"
d:DesignHeight="300" d:DesignWidth="400">

<StackPanel>
    <local:MyCustomControl Margin="10" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    <local:MyCustomControl Margin="10" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>

MainPage.xaml.cs

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }
}

Output

alt text

Leave a Comment