2010-10-20 112 views
1

在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

回答

1

這段代碼與你所擁有的沒有什麼不同。唯一的變化是字段LayoutRoot不是爲您創建的。

然而,隨着這行代碼: -

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

你的代碼的其餘部分將是相同的(雖然你應該測試LayoutRoot是否爲空第一)。

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

+0

謝謝@JSprang爲您的努力和時間,但我不得不更新我的問題。這真是令人困惑。 – nubm 2010-10-20 15:37:57