君子难得半日安

代码改变世界

在silverlight中对用户自定义模板的理解。

2013-08-14 10:20  半日安  阅读(147)  评论(0)    收藏  举报

在工作中遇到修改其他Telerik中选择颜色的控件进行扩展时,对这些问题进行的学习。

  1. 首先,我们要先交代两个概念:style和template.

       1.1 什么是style。

           style就是样式。样式是控件的样式,功能是来修改控件的属性。

           Style用于设置一种控件的属性(background,Width,template等),从而可以使这种控件调用style中设置的属性。

          (上面一条是网友给出的定义。)

       1.2 template的定义。

            模版,用于定义控件的内部结构,可以对控件的外观和形状进行改变。

           
       Control类型
        – Template属性 (ControlTemplate类型)
        – ContentPresenter
        – ContentTemplate (DataTemplate类型)

       ContentControl类型
         – Template属性 (ControlTemplate类型) 继承自Control
         – ContentTemplate (DataTemplate类型)

      ItemsControl类型
         – Template属性 (ControlTemplate类型) 继承自Control
         – ItemsPanel属性 (ItemsPanelTemplate类型) 指定布局容器
         – ItemTemplate属性 (DateTemplate类型) 每个Item的Template

 

      1.3 两者的区别。

        style只是用来修饰control,而template者是用来定义Control的结构。

 

   下面我贴段自定义控件的定义过程Template。摘抄地址:

   http://loekvandenouweland.com/index.php/2012/01/split-generic-xaml-in-silverlight-applications/

 

Split Generic.xaml in Silverlight Applications

by Loek on January 24, 2012

If you work with Templated controls in a big Silverlight project, your Generic.xaml might grow fast. Here’s a quick tutorial on how to split the Generic.xaml into multiple resource files.

Step1: Find the resource

You will typically have the control code:

public class TemplatedControl1 : Control {
    public TemplatedControl1() {
        this.DefaultStyleKey = typeof(TemplatedControl1);
    }
}

and the XAML in the Generic.xaml:

<Style TargetType="local:TemplatedControl1">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:TemplatedControl1">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Step2: Create a new resource file

Create a copy of Generic.xaml and rename to TemplatedControl1.xaml.
Delete the TemplatedControl1 style from Generic.xaml.
So Generic.xaml looks like:

<ResourceDictionary
    xmlns:local="clr-namespace:SilverlightApplication1">
</ResourceDictionary>

and TemplatedControl1.xaml looks like:

<ResourceDictionary
    xmlns:local="clr-namespace:SilverlightApplication1">
 
    <Style TargetType="local:TemplatedControl1">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:TemplatedControl1">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

Step3: Create reference in App.xaml

Open App.xaml and add the resource:

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Class="SilverlightApplication1.App"
             >
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source='/SilverlightApplication1;Component/Themes/TemplatedControl1.xaml'/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

If you open the project in Expression Blend you will see that you can edit the template as usual:

 

     现在,做silverlight的人越来越少了。希望这些对你们有用。

 

 

           

    

        

            

 

 

君子难得半日安