【问题标题】:How to create controls from code in a custom control?如何从自定义控件中的代码创建控件?
【发布时间】:2011-04-28 01:38:12
【问题描述】:

在 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 会更容易,但我很好奇如何从代码中做到这一点。

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

【问题讨论】:

  • 你能澄清你的问题吗?我不明白你在寻找什么。您想知道如何将 CustomControl 添加到您的 myStackPanel 中吗?或者您想知道如何使 myStackPanel 成为您添加到 LayoutRoot 的自定义控件?
  • 我很感兴趣如何以编程方式将 StackPanel 及其子项添加到我的自定义控件(Silverlight 库)中 - 即 MyDemoControl.cs(这里应该是带有 myStackPanel 的代码)+ Generic.xaml。也许我以完全错误的方式访问问题。
  • 所以你想做这样的事情:MyDemoControl 有一个按钮和一个 MyCustomControl,其中 MyCustomControl 有一些东西(如 StackPanel)?

标签: silverlight silverlight-4.0


【解决方案1】:

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

但是使用这行代码:-

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

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

【讨论】:

    【解决方案2】:

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

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

    输出

    【讨论】:

    • 感谢@JSprang 付出的努力和时间,但我必须更新我的问题。这真是令人困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    相关资源
    最近更新 更多