Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在silverlight中使用派生控件的内容_Silverlight_Templates_Inheritance_Silverlight 4.0_Custom Controls - Fatal编程技术网

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

如何在silverlight中使用派生控件的内容,silverlight,templates,inheritance,silverlight-4.0,custom-controls,Silverlight,Templates,Inheritance,Silverlight 4.0,Custom Controls,在我的项目中,许多视图必须在视图底部有“确定”、“取消”按钮。所以我想创建一个基本控件。并将“确定”、“取消”按钮添加到此控件的底部。然后我将继承此控件。在继承的控件中,我想在按钮附近添加一个文本框。我该怎么做?我相信基本控件有一个模板。在继承控件的模板中,编辑按钮面板并添加文本框。您可以创建从ContentControl继承的控件,并在其ControlTemplate中定义按钮。然后您就可以使用这个控件,如示例所示。 ControlWithButtons.cs [TemplatePart(Na

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

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

您可以创建从ContentControl继承的控件,并在其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”)


使用您的控件:

<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>

我找到了解决方案<代码>[ContentProperty(“”)属性解决了我的问题
像这样:

基本Xaml

继承的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>

我不知道你要做什么。。。是否要创建新控件,其中包含
Ok
Cancel
,并在这些按钮之后添加
TextBlock
?我找到了解决方案。[ContentProperty(“RootContentControl”)]
    <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>
[ContentProperty("RootContentControl")]//This attribute solved my problem.
public partial class BaseView : ChildWindow
{

    public BaseView()
    {
        InitializeComponent();
    }

    public UIElementCollection RootContentControl
    {
        get { return rootContent.Children; }
    }
}
<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>
public partial class InheritedView : BaseView
{
    public InheritedView()
    {
        InitializeComponent();
    }
}