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. (五)用正则化(Regularization)来解决过拟合

    1 过拟合 过拟合就是训练模型的过程中,模型过度拟合训练数据,而不能很好的泛化到测试数据集上.出现over-fitting的原因是多方面的: 1) 训练数据过少,数据量与数据噪声是成反比的,少量数据导 ...

  2. codeforce Codeforces Round #201 (Div. 2)

    cf 上的一道好题:  首先发现能生成所有数字-N 判断奇偶 就行了,但想不出来,如何生成所有数字,解题报告 说是  所有数字的中最大的那个数/所有数字的最小公倍数,好像有道理:纪念纪念: #incl ...

  3. ORACLE创建、修改、删除序列

    ORACLE没有象SQL SERVER中一样的自增加字段,要实现只能通过SEQUENCE来实现. 1.创建序列语法:(需要CREATE SEQUENCE系统权限) CREATE SEQUENCE 序列 ...

  4. 安装sass时,gem在国内不能安装的解决

    最近在安装SASS的时候,用到gem命令,但是运行出行如下错误!(先声明,安装sass前,要保证自己电脑安装了ruby:ruby -v可以测试下有没有装) 原因是ruby 的gem被和谐了,现在淘宝的 ...

  5. linux 开机自动运行

    1.开机启动时自动运行程序 Linux加载后, 它将初始化硬件和设备驱动, 然后运行第一个进程init.init根据配置文件继续引导过程,启动其它进程.通常情况下,修改放置在 /etc/rc或 /et ...

  6. std::vector数据复制

    std::vector<boost::shared_ptr <ITEM> > srcItemList;  // 数据源 std::vector<ITEM>  des ...

  7. 单机版搭建Hadoop环境图文教程详解

    安装过程: 一.安装Linux操作系统二.在Ubuntu下创建hadoop用户组和用户三.在Ubuntu下安装JDK四.修改机器名五.安装ssh服务六.建立ssh无密码登录本机七.安装hadoop八. ...

  8. 【LeetCode】66 & 67- Plus One & Add Binary

    66 - Plus One Given a non-negative number represented as an array of digits, plus one to the number. ...

  9. android 使用虚拟机安装apk(图文教程)(转)

    1.启动虚拟机 2.我的android sdk在“E:\android\android开发环境\android SDK\platform-tools”把要安装的apk复制到这个根目录(和adb.exe ...

  10. 在Cubieboard上关闭irqbalance服务避免内存泄漏

    十一一个假期回来,顺手看了看自己的cubieboard运行状态怎么样 aria2正常: btsync正常: samba正常: 很好, 顺手htop一下,已经开机了13天了,CPU使用率4%,内存使用率 ...