如何从自定义控件中的代码创建控件?

如何从自定义控件中的代码创建控件?

问题描述:

在MainPage.xaml.cs中(Silverlight应用程序),我可以做这样的事情:如何从自定义控件中的代码创建控件?

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

这是什么代码的时候我试图创建从这些代码控制自定义控件等同?

更新:

我的问题可能是太混乱了。我会尝试更好的配方。 所以,我有

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> 

和代码:

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"; 


    } 
} 

如果我在Button1的Click从内容变化“你好“到”你好按钮“。当Button1被点击时,我想要将带有两个按钮的StackPanel作为其子节点添加到Grid LayoutRootControl中。 我知道有Visibility属性并将其放入xaml会更容易,但我很好奇如何从代码中执行此操作。

我希望这比以前的问题更清楚。

+0

你能澄清你的问题吗?我不明白你在找什么。你想知道如何添加一个CustomControl到你的myStackPanel?或者你想知道如何让myStackPanel成为你添加到LayoutRoot的自定义控件? – JSprang 2010-10-20 14:23:14

+0

我很感兴趣如何以编程方式将其子项添加到我的自定义控件(Silverlight库) - 即MyDemoControl.cs(这里应该是myStackPanel的代码)+ Generic.xaml。也许我是以完全错误的方式访问问题的。 – nubm 2010-10-20 14:32:39

+0

所以你想要做这样的事情:MyDemoControl有一个按钮和一个MyCustomControl,MyCustomControl里面有东西(像一个StackPanel)? – JSprang 2010-10-20 14:35:18

这段代码与你所拥有的没有什么不同。唯一的变化是字段LayoutRoot不是为您创建的。

然而,随着这行代码: -

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

你的代码的其余部分将是相同的(虽然你应该测试LayoutRoot是否为空第一)。

在我看来,你只是想知道如何在多个地方使用自定义控件。

我已经创建了一个自定义控件(MyCustomControl),它具有代码中显示的StackPanel,然后在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(); 
    } 
} 

然后我用的是自定义控件两次在主视图。

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(); 
    } 
} 

输出

alt text

+0

谢谢@JSprang为您的努力和时间,但我不得不更新我的问题。这真是令人困惑。 – nubm 2010-10-20 15:37:57