TemplatePart作用

TemplatePart(Name="PART_Decrease", Type=typeof(RepeatButton))

一直没明白这是干嘛用的,搜了一下,记载一下。


以Button的定义为例:

[csharp]  view plain copy
  1. namespace System.Windows.Controls  
  2. {  
  3.     // Summary:  
  4.     //     Represents a button control, which reacts to the Click event.  
  5.     [TemplatePart(Name = "Normal State", Type = typeof(Storyboard))]  
  6.     [TemplatePart(Name = "MouseOver State", Type = typeof(Storyboard))]  
  7.     [TemplatePart(Name = "RootElement", Type = typeof(FrameworkElement))]  
  8.     [TemplatePart(Name = "Pressed State", Type = typeof(Storyboard))]  
  9.     [TemplatePart(Name = "FocusVisualElement", Type = typeof(UIElement))]  
  10.     [TemplatePart(Name = "Disabled State", Type = typeof(Storyboard))]  
  11.     public class Button : ButtonBase  
  12.     {  
  13.         // Summary:  
  14.         //     Initializes a new instance of the Button class.  
  15.         public Button();  
  16.         // Summary:  
  17.         //     Apply a template to the Button.  
  18.         protected override void OnApplyTemplate();  
  19.         //  
  20.         // Summary:  
  21.         //     Called when the IsEnabled property changes.  
  22.         //  
  23.         // Parameters:  
  24.         //   isEnabled:  
  25.         //     New value of the IsEnabled property.  
  26.         protected override void OnIsEnabledChanged(bool isEnabled);  
  27.     }  
  28. }  

 [TemplatePart(Name = "Normal State", Type = typeof(Storyboard))] 这种东东是做什么用的 , 其实这是一种契约 , 是一种推荐的控件设计模式(只是推荐) , 意思是告诉要来写ControlTemplate的用户 , 你的ControlTemplate中需要有一个x:Name为“Normal State” , 类型为Storyboard , 当然这个类型可以是继承来的, 为什么一定要包含这些契约规定的元素 , 因为逻辑部分对这些东西进行了引用,它们将对控件的默认行为起着关键作用, 可以理解为这个控件的最基本元素 , 是实现默认行为的最小集合, 自然,你的ControlTemplate中如果没有包含契约中的内容 , 则相应的逻辑将无法实现。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值