2012-07-13 88 views
0

我想创建一个新的Silverlight容器控件,控件应该包含两个按钮,默认情况下说一个保存和取消按钮。当用户在主页面上使用此控件时,他应该能够将新的控件添加到此控件上,例如文本框,组合等。此外,用户可以在代码中使用默认按钮(如btn_SaveClick和btn_CancelClick)的事件在主页的后面。是否可以创建这样的控件?
PS:我目前在VS2010上使用SilverLight5。自定义SilverLight容器控件

回答

0

这绝对有可能。首先,你需要自ContentControl派生的类:

public class MyControl : ContentControl ... 

然后,你需要类似下面的代码在你的XAML资源文件:

<!-- MyControl --> 
<Style TargetType="me:MyControl"> 
    <Setter Property="Foreground" Value="Black" /> 
    <Setter Property="BorderBrush" Value="Transparent" /> 
    <Setter Property="BorderThickness" Value="0" /> 
    <Setter Property="HorizontalAlignment" Value="Stretch" /> 
    <Setter Property="VerticalAlignment" Value="Stretch" /> 
    <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
    <Setter Property="VerticalContentAlignment" Value="Stretch" /> 
    <Setter Property="BorderMargin" Value="4,4,4,0" /> 
    <Setter Property="FooterMargin" Value="4,0,4,4" /> 

    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="me:MyControl"> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition /> 
         <RowDefinition Height="Auto" /> 
        </Grid.RowDefinitions> 

        <!-- Content --> 
        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> 

        <!-- Footer Buttons --> 
        <Grid x:Name="grdFooter" Grid.Row="1" Background="{StaticResource Footer_Bkg}" Margin="{TemplateBinding FooterMargin}"> 
         <!--Buttons here--> 
        </Grid> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

最后使用它在一个页面,你只需要像这样:

<me:MyControl x:Name="MainPage"> 
    <Grid x:Name="LayoutRoot"> 
     <!--Cool stuff here--> 
    </Grid> 
</me:MyControl> 
相关问题