如何在silverlight中使用派生控件的内容

时间:2011-12-08 10:19:52

标签: silverlight templates inheritance silverlight-4.0 custom-controls

在我的项目中,许多视图必须在视图底部有“确定”,“取消”按钮。所以我想创建一个基本控件。并在此控件的底部添加“确定”,“取消”按钮。然后我将继承这个控件。在继承的控件中,我想在文本框附近添加此按钮。我怎么能这样做?

3 个答案:

答案 0 :(得分:0)

我相信基础控件有一个模板。就这样,在继承控件的模板中编辑按钮面板并添加文本框。

答案 1 :(得分:0)

您可以创建从ContentControl继承的Control,并在ControlTemplate中定义您的按钮。然后您可以使用此控件,如示例所示。 ControlWithButtons.cs

[TemplatePart(Name="btnOk", Type= typeof(Button))]
public class ControlWithButtons : ContentControl
{
    public ControlWithButtons()
    {
        this.DefaultStyleKey = typeof(ControlWithButtons);
    }

    Button _btnOk;

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        _btnOk =  GetTemplateChild("btnOk") as Button;
        if (_btnOk != null)
        { 
            // do what you want with you button
            _btnOk.Click += new RoutedEventHandler(_btnOk_Click);
        }
    }

    void _btnOk_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Ok button clicked");
    }
}

Generic.xaml(必须在(ProjectDir)/Themes/Generic.xaml中) (不要忘记xmlns:local =“clr-namespace:Test”)

<Style TargetType="local:ControlWithButtons">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:ControlWithButtons">
                <Border Background="Yellow" CornerRadius="5">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="30"/>
                        </Grid.RowDefinitions>
                        <Border Margin="10" Background="LightGray">
                            <ContentPresenter/>
                        </Border>
                        <Button Grid.Row="1" x:Name="btnOk" Content="OK" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

使用您的控件:

<Grid x:Name="LayoutRoot" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <local:ControlWithButtons Width="300" Height="250" HorizontalAlignment="Center" VerticalAlignment="Center">

        <!-- TextBox is put into control -->
        <TextBox Width="200" Height="Auto" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5" />

        <!-- You can also specify ContentTemplate for ControlWithButtons  -->
        <!-- (see in MSDN "http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.contenttemplate(v=vs.95).aspx") -->

    </local:ControlWithButtons>
</Grid>

答案 2 :(得分:0)

我找到了解决方案。 [ContentProperty("")]属性解决了我的问题。
像这样:

基础Xaml     

    <Grid Name="layoutRoot" VerticalAlignment="Stretch"  >
        <Grid.RowDefinitions>
            <RowDefinition Height="289*" />
            <RowDefinition Height="51" />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="1" Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Orientation="Horizontal">
            <Button Name="btnOk" Content="Ok" Height="23"  Width="75"  HorizontalAlignment="Left" Margin="10,10,10,10"  Command="{Binding Path=OnApply, Mode=OneWay}"/>
            <Button Name="btnCancel" Content="Cancel" Height="23"  Width="75"  HorizontalAlignment="Left" Margin="10,10,10,10"/>
        </StackPanel>
        <Grid Name="rootContent" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Column="0" Grid.Row="0" />
    </Grid>
</sdk:ChildWindow>

Base CodeBehind

[ContentProperty("RootContentControl")]//This attribute solved my problem.
public partial class BaseView : ChildWindow
{

    public BaseView()
    {
        InitializeComponent();
    }

    public UIElementCollection RootContentControl
    {
        get { return rootContent.Children; }
    }
}

继承Xaml

<Views:BaseView x:Class="MvvmLight1.Views.InheritedView"
             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"
             xmlns:Views="clr-namespace:MvvmLight1.Views" 
                xmlns:viewModels="clr-namespace:MvvmLight1.ViewModel"                
                mc:Ignorable="d" d:DesignHeight="244" d:DesignWidth="392"
                DataContext="{Binding Source=viewModels:InheritedViewModel}">
    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
        <Grid.RowDefinitions>
            <RowDefinition />
        </Grid.RowDefinitions>
    </Grid>
</Views:BaseView>

继承CodeBehind

public partial class InheritedView : BaseView
{
    public InheritedView()
    {
        InitializeComponent();
    }
}