博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF知识点全攻略09- 附加属性
阅读量:4984 次
发布时间:2019-06-12

本文共 5367 字,大约阅读时间需要 17 分钟。

附加属性也是一种特殊的依赖属性。

Canvas中的Canvas.Left,Canvas.Top ,DockPanel中DockPanel.Dock等就是附加属性。

更加.NET类属性的写法经验。这个中可以直接点出来的,都是不用实例化的静态的。以Top属性为例:

public static readonly DependencyProperty TopProperty =    DependencyProperty.RegisterAttached("Top",     typeof(double), typeof(Canvas),    new FrameworkPropertyMetadata(0d,        FrameworkPropertyMetadataOptions.Inherits)); public static void SetTop(UIElement element, double value){    element.SetValue(TopProperty, value);} public static double GetTop(UIElement element){    return (double)element.GetValue(TopProperty); }

 看如下代码效果:

代码中可以看到, Convas.Left附加属性实际作用在 Rectangle在Canvas中的Left属性上,Convas.Top附加属性实际作用在 Rectangle在Canvas中的Top属性上。

再来看一个例子:

通过Grid的附加属性Clip我们截取到了想要的图片效果。

 那么什么时候去使用附加属性呢?下面是WPF经典Material Design主题开源项目Material Design In XAML Toolkit中对于阴影部分处理的源码。

public static class ShadowAssist    {        public static readonly DependencyProperty ShadowDepthProperty = DependencyProperty.RegisterAttached(            "ShadowDepth", typeof (ShadowDepth), typeof (ShadowAssist), new FrameworkPropertyMetadata(default(ShadowDepth), FrameworkPropertyMetadataOptions.AffectsRender));        public static void SetShadowDepth(DependencyObject element, ShadowDepth value)        {            element.SetValue(ShadowDepthProperty, value);        }        public static ShadowDepth GetShadowDepth(DependencyObject element)        {            return (ShadowDepth) element.GetValue(ShadowDepthProperty);        }        private static readonly DependencyPropertyKey LocalInfoPropertyKey = DependencyProperty.RegisterAttachedReadOnly(            "LocalInfo", typeof (ShadowLocalInfo), typeof (ShadowAssist), new PropertyMetadata(default(ShadowLocalInfo)));        private static void SetLocalInfo(DependencyObject element, ShadowLocalInfo value)        {            element.SetValue(LocalInfoPropertyKey, value);        }        private static ShadowLocalInfo GetLocalInfo(DependencyObject element)        {            return (ShadowLocalInfo) element.GetValue(LocalInfoPropertyKey.DependencyProperty);        }        public static readonly DependencyProperty DarkenProperty = DependencyProperty.RegisterAttached(            "Darken", typeof (bool), typeof (ShadowAssist), new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.AffectsRender, DarkenPropertyChangedCallback));        private static void DarkenPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)        {            var uiElement = dependencyObject as UIElement;            var dropShadowEffect = uiElement?.Effect as DropShadowEffect;            if (dropShadowEffect == null) return;            if ((bool) dependencyPropertyChangedEventArgs.NewValue)            {                SetLocalInfo(dependencyObject, new ShadowLocalInfo(dropShadowEffect.Opacity));                var doubleAnimation = new DoubleAnimation(1, new Duration(TimeSpan.FromMilliseconds(350)))                {                    FillBehavior = FillBehavior.HoldEnd                };                dropShadowEffect.BeginAnimation(DropShadowEffect.OpacityProperty, doubleAnimation);                            }            else            {                var shadowLocalInfo = GetLocalInfo(dependencyObject);                if (shadowLocalInfo == null) return;                var doubleAnimation = new DoubleAnimation(shadowLocalInfo.StandardOpacity, new Duration(TimeSpan.FromMilliseconds(350)))                {                    FillBehavior = FillBehavior.HoldEnd                };                dropShadowEffect.BeginAnimation(DropShadowEffect.OpacityProperty, doubleAnimation);            }        }        public static void SetDarken(DependencyObject element, bool value)        {            element.SetValue(DarkenProperty, value);        }        public static bool GetDarken(DependencyObject element)        {            return (bool) element.GetValue(DarkenProperty);        }        public static readonly DependencyProperty CacheModeProperty = DependencyProperty.RegisterAttached(            "CacheMode", typeof(CacheMode), typeof(ShadowAssist), new FrameworkPropertyMetadata(new BitmapCache { EnableClearType = true, SnapsToDevicePixels = true }, FrameworkPropertyMetadataOptions.Inherits));        public static void SetCacheMode(DependencyObject element, CacheMode value)        {            element.SetValue(CacheModeProperty, value);        }        public static CacheMode GetCacheMode(DependencyObject element)        {            return (CacheMode)element.GetValue(CacheModeProperty);        }        public static readonly DependencyProperty ShadowEdgesProperty = DependencyProperty.RegisterAttached(            "ShadowEdges", typeof(ShadowEdges), typeof(ShadowAssist), new PropertyMetadata(ShadowEdges.All));        public static void SetShadowEdges(DependencyObject element, ShadowEdges value)        {            element.SetValue(ShadowEdgesProperty, value);        }        public static ShadowEdges GetShadowEdges(DependencyObject element)        {            return (ShadowEdges) element.GetValue(ShadowEdgesProperty);        }    }

谷歌提出的“材料设计” 理念中,阴影是比较重要的一部分,而该项目就是使用了附加属性来达到了效果。其中还有不少特性也是通过附加属性来完成的,给我如何使用附加属性,提供了比较好的模板。

可以参考学习:

转载于:https://www.cnblogs.com/kuangxiangnice/p/11071843.html

你可能感兴趣的文章
HDU 2604
查看>>
.NET Core微服务之基于Exceptionless实现分布式日志记录
查看>>
unity3d IL2CPP for android
查看>>
(一) Go的基本类型
查看>>
PHP程序员的技术成长规划
查看>>
使用moment.js轻松管理日期和时间
查看>>
大白话系列之C#委托与事件讲解(三)
查看>>
php分享十五:php的命令行操作
查看>>
团体程序设计天梯赛-练习集-L1-035. 情人节
查看>>
PAT-树的同构
查看>>
【C和指针】数据
查看>>
EF6+MySql 软件配置环境 EF连接不到mysql问题 实体数据模型向导 选不到mysql
查看>>
《Head first设计模式》学习笔记 – 迭代器模式
查看>>
大家赶快升级到quartus ii 11.0吧,现在文字编辑器又支持中文啦
查看>>
方法该返回接口还是具体类,以及面向接口编程
查看>>
[BTS] Error in Check Transaction: 没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))...
查看>>
数据库范式
查看>>
mysql存储引擎innodb、myisam区别
查看>>
设置和获取cookie
查看>>
hdu5716
查看>>