简体   繁体   English

具有模板支持的Windows Phone自定义用户控件

[英]Windows phone custom user control with template support

I am making a custom user control and I want the control to be reusable and a part of the control to be usable for adding new controls. 我正在制作一个自定义用户控件,我希望该控件可重用,并且该控件的一部分可用于添加新控件。 What I am trying to make is a template in the custom user control in which the user can add new content. 我要制作的是自定义用户控件中的模板,用户可以在其中添加新内容。

I am using windows phone 8 我正在使用Windows Phone 8

How can I do this? 我怎样才能做到这一点?

Add new Templated Control from "Add new item" menu. 从“添加新项”菜单中添加新的模板化控件。 You should get Generic.xaml file in Themes folder. 您应该在Themes文件夹中获取Generic.xaml文件。

In Generic.xaml you have style for your custom control: Generic.xaml您具有自定义控件的样式:

<Style TargetType="local:CustomControl1">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:CustomControl1">
                Write your control xaml here
                <Border x:Name="BorderNameTest"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <Button IsEnabled="{TemplateBinding IsFancyLookEnabled}"></Button>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

You'll also have .cs file for your custom control. 您还将具有用于自定义控件的.cs文件。 To use controls (in code) from your custom control template xaml you need to use [TemplatePart] attribute on your "control class". 要使用自定义控件模板xaml中的控件(以代码形式),您需要在“控件类”上使用[TemplatePart]属性。 Snippet: 片段:

[TemplatePart(Name = BorderTestTemplatePartName, Type = typeof(Border))]
public sealed class CustomControl1 : Control
{
    private const string BorderTestTemplatePartName = "BorderNameTest";
    private Border _myBorder;

   public static readonly DependencyProperty IsFancyLookEnabledProperty = DependencyProperty.Register(
        "IsFancyLookEnabled", typeof (bool), typeof (CustomControl1), new PropertyMetadata(default(bool)));

    public bool IsFancyLookEnabled
    {
        get { return (bool) GetValue(IsFancyLookEnabledProperty); }
        set { SetValue(IsFancyLookEnabledProperty, value); }
    } 

    public CustomControl1()
    {
        this.DefaultStyleKey = typeof(CustomControl1);
    }

    protected override void OnApplyTemplate()
    {
        _myBorder = GetTemplateChild(BorderTestTemplatePartName) as Border;

        // attach events etc. (you can detach them using for example Unloaded event)

        base.OnApplyTemplate();
    }
}

Additionaly I have shown you how to expose properties on your control (so your control user can write <namespace:SuperControl IsFancyLookEnabled="True"> ). 另外,我向您展示了如何在控件上公开属性(以便控件用户可以编写<namespace:SuperControl IsFancyLookEnabled="True"> )。 You create dependency property (as shown in code snippet) which you can use in your xaml with TemplateBinding - or just use in code. 您创建了依赖项属性(如代码片段所示),您可以在具有TemplateBinding的xaml中使用该属性,也可以仅在代码中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM