Limited functionality:

  • Not settable
  • No data binding
  • No validation
  • No animation
  • No Inheritance

When to use?

  • Used for state determination(Defined the style for the control, some visiable property like fore/background will change when mouse movehover)
  • Used as a property trigger(由这个readonly property change,来change control's states)

要求: 我们有一个button,和一个read-only property来track button is been clicked的状态,如果被clicked的话,这个property变为true,我们change button的background

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14.  
  15. namespace CustomControlLib
  16. {
  17. public class MyCustomControl : Control
  18. {
  19. static MyCustomControl()
  20. {
  21. DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));
  22. }
  23. private static readonly DependencyPropertyKey HasBeenClickedPropertyKey = DependencyProperty.RegisterReadOnly("HasBeenClicked",
  24. typeof(bool), typeof(MyCustomControl), new PropertyMetadata(false));
  25. public static readonly DependencyProperty HasBeenClickedProperty = HasBeenClickedPropertyKey.DependencyProperty;
  26. //When you create the DependencyPropertyKey it will automaticly create the DependencyProperty for you
  27.  
  28. public bool HasBeenClicked //注意这里的wrapper的名字要和register时给的""里的名字一致
  29. {
  30. get { return (bool)GetValue(HasBeenClickedProperty); } //因为做的是 readonly property,所以只有getter
  31. }
  32.  
  33. public override void OnApplyTemplate() //override OnApplyTemplate
  34. {
  35. base.OnApplyTemplate();
  36.  
  37. //demo purposes only, check for previous instances and remove handler first
  38. var button = GetTemplateChild("PART_Button") as Button; //call 前台的x:Name
  39. if (button != null)
  40. button.Click += Button_Click; //hock OnApplyTemplate() 去instantiate 一个按钮被点击的Event Handler
  41.  
  42. }
  43.  
  44. void Button_Click(object sender, RoutedEventArgs e)
  45. {
  46. SetValue(HasBeenClickedPropertyKey, true); //其他继承类若要用这个DependencyPropertyKey,吧private改成protect或者internal
  47. //接下来要做的事当这个button被按后我们要set HasBeenClicked(ReadOnly)
  48. //不可以set HasBeenClicked只可以用SetValue找HasBeenClickedPropertyKey
  49. }
  50. }
  51. }
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns:local="clr-namespace:CustomControlLib">
  4.  
  5. <Style TargetType="{x:Type local:MyCustomControl}">
  6. <Setter Property="Margin" Value="50" />
  7. <Setter Property="Background" Value="Gray" />
  8. <Setter Property="Template">
  9. <Setter.Value>
  10. <ControlTemplate TargetType="{x:Type local:MyCustomControl}">
  11. <Border Background="{TemplateBinding Background}"
  12. BorderBrush="{TemplateBinding BorderBrush}"
  13. BorderThickness="{TemplateBinding BorderThickness}">
  14.  
  15. <Button x:Name="PART_Button"
  16. Background="{TemplateBinding Background}"
  17. Content="Click Me" />
  18.  
  19. </Border>
  20. <ControlTemplate.Triggers>
  21. <Trigger Property="HasBeenClicked" Value="true">
  22. <Setter Property="Background" Value="Green" />
  23. </Trigger>
  24.  
  25. </ControlTemplate.Triggers>
  26. </ControlTemplate>
  27. </Setter.Value>
  28. </Setter>
  29. </Style>
  30. </ResourceDictionary>
  1. <Window x:Class="CustomControlDemo.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:cc="clr-namespace:CustomControlLib;assembly=CustomControlLib"
  5. Title="MainWindow" Height="350" Width="525">
  6. <Grid>
  7. <cc:MyCustomControl />
  8. </Grid>
  9. </Window>

Custom ReadOnlyProperty【PluraSight】的更多相关文章

  1. UserControl和CustomControl基础【PluraSight】

    UserControl UserControl实际上就是ContentControl,xaml里<UserControl></UserControl>tag之间包含的实际就是后 ...

  2. TemplateBinding vs TemplatedParent【PluraSight】

    TemplateBinding:TemplateBinding是一个Markup Extension

  3. 【七】注入框架RoboGuice使用:(Your First Custom Binding)

    上一篇我们简单的介绍了一下RoboGuice的使用([六]注入框架RoboGuice使用:(Singletons And ContextSingletons)),今天我们来看下自己定义绑定(bindi ...

  4. 【五】将博客从jekyll迁移到了hexo

    本系列有五篇:分别是  [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面  [二]jekyll 的使用 :主要是jekyll的配置  [三]Markdo ...

  5. 【二】jekyll 的使用

    本系列有五篇:分别是 [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面 [二]jekyll 的使用 :主要是jekyll的配置 [三]Markdown+ ...

  6. 【转】jqGrid 各种参数 详解

      [原文]http://www.cnblogs.com/younggun/archive/2012/08/27/2657922.htmljqGrid 各种参数 详解 JQGrid JQGrid是一个 ...

  7. 【Bootstrap】2.作品展示站点

    假设我们已经想好了要给自己的作品弄一个在线站点.一如既往,时间紧迫.我们需要快一点,但作品展示效果又必须专业.当然,站点还得是响应式的,能够在各种设备上正常浏览,因为这是我们向目标客户推销时的卖点.这 ...

  8. 【开源】分享一个前后端分离方案-前端angularjs+requirejs+dhtmlx 后端asp.net webapi

    一.前言 半年前左右折腾了一个前后端分离的架子,这几天才想起来翻出来分享给大家.关于前后端分离这个话题大家也谈了很久了,希望我这个实践能对大家有点点帮助,演示和源码都贴在后面. 二.技术架构 这两年a ...

  9. 【转载】【Oracle 11gR2】db_install.rsp详解

    [原文]http://blog.csdn.net/jameshadoop/article/details/48086933 ###################################### ...

随机推荐

  1. Javaweb里面的filter,listener,servlet

    Filter 1Filter是什么:是过滤器简称 2Filter有什么作用:在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装 ...

  2. 【转】iOS 开发之协议protocal-代理传值delegate

    原文网址:http://www.cnblogs.com/wzrong/p/3201938.html 刚开始做iOS开发的时候,对 protocol.delegate 的理解一直都是晕晕乎乎一知半解的状 ...

  3. 五个JS经典面试题

    1:Scope作用范围 1: (function() { 2: var a = b = 5; 3: })(); 4: 5: console.log(b); 什么会被打印在控制台上? 回答 上面的代码会 ...

  4. AJax学习一

    1.Ajax的准备工作,就是要先准备好服务器环境. 这边可以有好几种方式,例如服务器工具: http://www.php100.com/html/plugin/ser/2013/0905/91.htm ...

  5. 嵌入式 hi3518平台检测网线是否插上

    /********************************** (C) COPYRIGHT ******************************* * File Name        ...

  6. ueditor的工具栏显示乱码解决方法 小问题.. 是你的页面编码与语言包js编码不符所导致的

    ueditor的工具栏显示乱码解决方法 小问题..  是你的页面编码与语言包js编码不符所导致的解决方法:用记事本将ueditor\..\lang\zh-cn\zh-cn.js打开,然后保存为ANSI ...

  7. ADO.NET+Access: 2,至少一个参数没有被指定值

    ylbtech-Error-ADO.NET+Access: 2,至少一个参数没有被指定值. 1.A,错误代码返回顶部  2,至少一个参数没有被指定值. 1.B,出错原因分析返回顶部  未解决 1.C, ...

  8. ADO.NET+Access: 1,标准表达式中数据类型不匹配

    ylbtech-Error-ADO.NET+Access: 1,标准表达式中数据类型不匹配. 1.A,错误代码返回顶部  1,标准表达式中数据类型不匹配. 1.B,出错原因分析返回顶部  未解决 1. ...

  9. 解决:cc1.exe: sorry, unimplemented: 64-bit mode not compiled in

    在win下用Go语言的cgo时(比如下面场景)我们会用到的GCC编译器,Win下我们一般用MinGW. Golang连接Oracle数据库:win下 golang 跨平台编译 MinGW全称Minim ...

  10. DuiLib消息处理剖析

    本来想自己写写duilib的消息机制来帮助duilib的新手朋友,不过今天发现已经有人写过了,而且写得很不错,把duilib的主干消息机制都说明了,我就直接转载过来了,原地址:http://blog. ...