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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace CustomControlLib
{
public class MyCustomControl : Control
{
static MyCustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));
}
private static readonly DependencyPropertyKey HasBeenClickedPropertyKey = DependencyProperty.RegisterReadOnly("HasBeenClicked",
typeof(bool), typeof(MyCustomControl), new PropertyMetadata(false));
public static readonly DependencyProperty HasBeenClickedProperty = HasBeenClickedPropertyKey.DependencyProperty;
//When you create the DependencyPropertyKey it will automaticly create the DependencyProperty for you public bool HasBeenClicked //注意这里的wrapper的名字要和register时给的""里的名字一致
{
get { return (bool)GetValue(HasBeenClickedProperty); } //因为做的是 readonly property,所以只有getter
} public override void OnApplyTemplate() //override OnApplyTemplate
{
base.OnApplyTemplate(); //demo purposes only, check for previous instances and remove handler first
var button = GetTemplateChild("PART_Button") as Button; //call 前台的x:Name
if (button != null)
button.Click += Button_Click; //hock OnApplyTemplate() 去instantiate 一个按钮被点击的Event Handler } void Button_Click(object sender, RoutedEventArgs e)
{
SetValue(HasBeenClickedPropertyKey, true); //其他继承类若要用这个DependencyPropertyKey,吧private改成protect或者internal
//接下来要做的事当这个button被按后我们要set HasBeenClicked(ReadOnly)
//不可以set HasBeenClicked只可以用SetValue找HasBeenClickedPropertyKey
}
}
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomControlLib"> <Style TargetType="{x:Type local:MyCustomControl}">
<Setter Property="Margin" Value="50" />
<Setter Property="Background" Value="Gray" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyCustomControl}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"> <Button x:Name="PART_Button"
Background="{TemplateBinding Background}"
Content="Click Me" /> </Border>
<ControlTemplate.Triggers>
<Trigger Property="HasBeenClicked" Value="true">
<Setter Property="Background" Value="Green" />
</Trigger> </ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
<Window x:Class="CustomControlDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:CustomControlLib;assembly=CustomControlLib"
Title="MainWindow" Height="350" Width="525">
<Grid>
<cc:MyCustomControl />
</Grid>
</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. poj 2661 Factstone Benchmark (Stirling数)

    //题意是对于给定的x,求满足n! <= 2^(2^x)的最大的n//两边同取以二为底的对数,可得: lg2(n!) <= 2^x 1.   log2(n!) = log2(1) + lo ...

  2. Oracle时间函数numtoyminterval()

    格式:NumToYMInterval(n, interval_unit); n: 数值类型 interval_unit: 'YEAR', 'MONTH' ,或其他可以转换成这两个值之一的表达式   N ...

  3. Python使用os.listdir()函数来得目录内容的介绍

    Python编程语言是计算机语言中常用的语言,以下的文章就是介绍在Python编程语言中使用os.listdir()函数来获得目录中的相关内容的介绍,如果你对其相关的实际操作有兴趣的话,你就可以观看以 ...

  4. mysql 添加索引后 在查询的时候是mysql就自动从索引里面查询了。还是查询的时候有单 独的参数查询索引?

    MYSQL在创建索引后对索引的使用方式分为两种:1 由数据库的查询优化器自动判断是否使用索引:2 用户可在写SQL语句时强制使用索引 下面就两种索引使用方式进行说明第一种,自动使用索引.数据库在收到查 ...

  5. 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现

    一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...

  6. GC Buffer Busy Waits处理(转载)

    与单实例不同,在RAC环境中,由于多节点的原因,会因为节点间的资源争用产生GC类的等待,而这其中,GC Buffer Busy Waits又是最为常见的,从性能角度上说,RAC是把双刃剑,用的好,能够 ...

  7. Python 异常相关参考

    Python所有的异常都是从BaseException类派生的,常见的错误类型和继承关系如下: BaseException +-- SystemExit +-- KeyboardInterrupt + ...

  8. Json.Net 使用属性定义日期的序列化格式

    如果一个实体类里所有的时间即DateTime类型的字段,都处理成统一格式的话,可以使用如下方式: IsoDateTimeConverter timeFormat = new IsoDateTimeCo ...

  9. Chapter13:拷贝控制

    拷贝控制操作:拷贝构造函数.拷贝赋值运算符.移动构造函数.移动赋值运算符.析构函数. 实现拷贝控制操作的最困难的地方是首先认识到什么时候需要定义这些操作. 拷贝构造函数: 如果一个构造函数的第一个参数 ...

  10. 一款APP从设计稿到切图过程全方位揭秘 Mark

    纯干货!一款APP从设计稿到切图过程全方位揭秘   @BAT_LCK:我本身是一名GUI设计师,所以我只站在GUI设计师的角度去把APP从项目启动到切片输出的过程写一写,相当于工作流程的介绍吧.公司不 ...