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

时间:2010-10-20 13:43:37

标签: silverlight silverlight-4.0

在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,内容会从“Hi”变为“Hello Button”。我想,当单击Button1时,将带有两个按钮的StackPanel作为其子项添加到Grid LayoutRootControl中。 我知道有Visibility属性并将它放入xaml会更容易,但我很好奇如何从代码中做到这一点。

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

2 个答案:

答案 0 :(得分:1)

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

但是这行代码: -

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

您的其余代码将是相同的(尽管您应首先测试LayoutRoot是否为null)。

答案 1 :(得分:0)

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

我已经创建了一个自定义控件(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