Custom ReadOnlyProperty【PluraSight】
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】的更多相关文章
- UserControl和CustomControl基础【PluraSight】
UserControl UserControl实际上就是ContentControl,xaml里<UserControl></UserControl>tag之间包含的实际就是后 ...
- TemplateBinding vs TemplatedParent【PluraSight】
TemplateBinding:TemplateBinding是一个Markup Extension
- 【七】注入框架RoboGuice使用:(Your First Custom Binding)
上一篇我们简单的介绍了一下RoboGuice的使用([六]注入框架RoboGuice使用:(Singletons And ContextSingletons)),今天我们来看下自己定义绑定(bindi ...
- 【五】将博客从jekyll迁移到了hexo
本系列有五篇:分别是 [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面 [二]jekyll 的使用 :主要是jekyll的配置 [三]Markdo ...
- 【二】jekyll 的使用
本系列有五篇:分别是 [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面 [二]jekyll 的使用 :主要是jekyll的配置 [三]Markdown+ ...
- 【转】jqGrid 各种参数 详解
[原文]http://www.cnblogs.com/younggun/archive/2012/08/27/2657922.htmljqGrid 各种参数 详解 JQGrid JQGrid是一个 ...
- 【Bootstrap】2.作品展示站点
假设我们已经想好了要给自己的作品弄一个在线站点.一如既往,时间紧迫.我们需要快一点,但作品展示效果又必须专业.当然,站点还得是响应式的,能够在各种设备上正常浏览,因为这是我们向目标客户推销时的卖点.这 ...
- 【开源】分享一个前后端分离方案-前端angularjs+requirejs+dhtmlx 后端asp.net webapi
一.前言 半年前左右折腾了一个前后端分离的架子,这几天才想起来翻出来分享给大家.关于前后端分离这个话题大家也谈了很久了,希望我这个实践能对大家有点点帮助,演示和源码都贴在后面. 二.技术架构 这两年a ...
- 【转载】【Oracle 11gR2】db_install.rsp详解
[原文]http://blog.csdn.net/jameshadoop/article/details/48086933 ###################################### ...
随机推荐
- Python 删除 数组
numpy删除一列 从0开始,第三个参数是第几个维度 可以多删几个
- 在Android中使用并发来提高速度和性能
Android框架提供了很实用的异步处理类.然而它们中的大多数在一个单一的后台线程中排队.当你需要多个线程时你是怎么做的? 众所周知,UI更新发生在UI线程(也称为主线程).在主线程中的任何操作都会阻 ...
- web app 页面旋转整体样式问题
$(window).bind("orientationchange", function (event) { if (event.orientation) { //portrait ...
- php.curl详解
目前为目最全的CURL中文说明了,学PHP的要好好掌握.有很多的参数.大部份都很有用.真正掌握了它和正则,一定就是个采集高手了. PHP中的CURL函数库(Client URL Library Fun ...
- c# is和as的区别
关于类型的判断和转换有is和as这2个操作符.具体区别和用法如下is就是处于对类型的判断.返回true和false.如果一个对象是某个类型或是其父类型的话就返回为true,否则的话就会返回为false ...
- ylb:exists(存在)的应用实例
ylbtech-SQL Server:exists(存在)的应用实例 SQL Server exists(存在)的应用实例. 1,exists(存在)的应用实例 返回顶部 -- =========== ...
- show engine innodb status 详解
找个mysql客户端,执行show engine innodb status得到如下结果: 详细信息如下: ************************************** ======= ...
- 基于opencv的手写数字识别(MFC,HOG,SVM)
参考了秋风细雨的文章:http://blog.csdn.net/candyforever/article/details/8564746 花了点时间编写出了程序,先看看效果吧. 识别效果大概都能正确. ...
- CSS的优先级规则
CSS的优先级规则有两类 1.位置群组规则 最高优先级为元素内嵌的style样式,如<div style=” “></div> 次高优先级为html头部中的<style& ...
- 解决ext时间插件在谷歌下变宽的BUG
在做一个项目时候遇到EXT这么一个问题,现分享出解决问题的代码 Ext.override(Ext.menu.DateMenu, { render: function () { Ext.menu.Dat ...