Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create controls from code in a custom control?
    primarykey
    data
    text
    <p>In MainPage.xaml.cs (Silverlight Application) I can do something like this:</p> <pre><code>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); </code></pre> <p>What is the equivalent of this code in a custom control when I'm trying to create these controls from the code?</p> <p><strong>Update:</strong></p> <p>My question is probably too confusing. I'l try better formulation. So, I have</p> <p><strong>Generic.xaml</strong></p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DemoAddControlLib"&gt; &lt;Style TargetType="local:DemoControlShowtime"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="local:DemoControlShowtime"&gt; &lt;Grid x:Name="LayoutRootControl"&gt; &lt;Button x:Name="Button1" Content="Hi" Width="150" Height="30"&gt;&lt;/Button&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/ResourceDictionary&gt; </code></pre> <p>And code:</p> <p><strong>DemoControlShowtime.cs</strong></p> <pre><code>[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"; } } </code></pre> <p>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.</p> <p>I hope this is much clearer than the question was before.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload