logo资料库

WPF经典教程之依赖项属性和路由事件.doc

第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
资料共7页,全文预览结束
一、依赖项属性(Dependency Property)
(更多资源:
一、 依赖项属性(Dependency Property) (更多资源:http://cleopard.download.csdn.net/) Windows Presentation Foundation (WPF) 提供了一组服务,这些服务可 用于扩展公共语言运行时 (CLR) 属性的功能。这些服务通常统称为 WPF 属性 系统。由 WPF 属性系统支持的属性称为依赖项属性。本概述介绍 WPF 属性 系统以及依赖项属性的功能,这包括如何在可扩展应用程序标记语言 (XAML) 中和代码中使用现有的依赖项属性。 依赖项属性的用途在于提供一种方法来基于其他输入的值计算属性值。这些其他 输入可以包括系统属性(如主题和用户首选项)、实时属性确定机制(如数据绑 定和动画/演示图板)、重用模板(如资源和样式)或者通过与元素树中其他元 素的父子关系来公开的值。另外,可以通过实现依赖项属性来提供独立验证、默 认值、监视其他属性的更改的回调以及可以基于可能的运行时信息来强制指定属 性值的系统。派生类还可以通过重写依赖项属性元数据(而不是重写现有属性的 实际实现或者创建新属性)来更改现有属性的某些具体特征。 1、依赖项属性与 CLR 包装属性 以 Button 的 Backgroud 为例,设置或获取其值可以有以下几种方式: XAML 文件中 在所生成的代码中,XAML 加载器将 XAML 属性的简单字符串 类型转换为 WPF 类型(一种 Color,通过 SolidColorBru -->
15: 16: 17: 18: 19: _ButtonC" /> _ButtonD" />
      数据绑定 样式 动画 元数据重写 属性值继承 WPF 设计器集成 3、自定义依赖项属性及重写依赖项属性 对于自定义依赖项属性,其所在的类型必须直接或间接继承 System.Windows. DependencyObject 类,依赖项属性是通过调用 Register 方法(或 Regist erReadOnly,自定义的只读的依赖项属性)在 WPF 属性系统中注册,并通过 DependencyProperty 标识符字段备份的属性。依赖项属性只能由 Depend encyObject 类型使用,但 DependencyObject 在 WPF 类层次结构中的级 别很高,因此,WPF 中的大多数可用类都支持依赖项属性。在对依赖项属性及 CLR 包装属性命名时必须满足:CLR 包装属性名+Property=依赖项属性名。 例如:在某 DependencyObject 类的子类中定义: 1: // 定义并注册依赖项属性 2: public static readonly DependencyProperty AquariumGra phicProperty = 3: 4: 对象的名称 5: 6: 象的所有者类型 DependencyProperty.Register( "AquariumGraphic", // 要注册的依赖项 typeof(Uri), // 属性的类型 typeof(AquariumObject), // 正注册依赖项对 7: new FrameworkPropertyMetadata( // 依赖项对象的 属性元数据 null, FrameworkPropertyMetadataOptions.AffectsRe new PropertyChangedCallback(OnUriChanged) 8: 9: nder, 10: 11: 12: 13: 14: // 定义 CLR 包装属性 ); )
15: public Uri AquariumGraphic 16: { 17: y); } 18: 19: } get { return (Uri)GetValue(AquariumGraphicPropert set { SetValue(AquariumGraphicProperty, value); } 二、路由事件(RoutedEvent) 先看以下的应用程序: 1: 2: panel_Blue">
10: 11: 12: 13: 14: string.Format("sender Type is {0}.", sender.Ge tType().Name)); Button sourceButton = (Button) (sender); MessageBox.Show( string.Format("Source Button is {0} !", source Button.Name), "System Infomation", MessageBoxButton.OK, MessageBoxImage.Information); 15: 16: 17: 18: } 19: 20: private void panel_G_Click(object sender, RoutedEven MessageBox.Show( "Click Green Panel Button !", "System Infomation", MessageBoxButton.OK, MessageBoxImage.Information); tArgs e) 21: { 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: tArgs e) 43: { 44: 45: 46: MessageBox.Show( string.Format("sender Type is {0}.", sender.Ge tType().Name)); MessageBox.Show( string.Format("e.Source Type is {0}.", e.Sourc e.GetType().Name)); Button sourceButton = (Button)(e.Source); MessageBox.Show( string.Format("Source Button is {0} !", source Button.Name), "System Infomation", MessageBoxButton.OK, MessageBoxImage.Information); 37: 38: 39: 40: } 41: 42: private void panel_R_Click(object sender, RoutedEven MessageBox.Show( "Click Red Panel Button !", "System Infomation",
MessageBoxButton.OK, MessageBoxImage.Information); 47: 48: 49: } 50: 51: private void btn_R1_Click(object sender, RoutedEvent MessageBox.Show( Args e) 52: { 53: 54: 55: 56: 57: 58: } 59: 60: private void btn_R2_Click(object sender, RoutedEvent "Click Red Panel Button A !", "System Infomation", MessageBoxButton.OK, MessageBoxImage.Information); Args e) 61: { 62: 63: 64: 65: 66: 67: 68: 69: } MessageBox.Show( "Click Red Panel Button B !", "System Infomation", MessageBoxButton.OK, MessageBoxImage.Information); e.Handled = true; 1、在 panel_Blue 中定义的三个按钮的 Click 事件属于“类事件”,即在类型对 象中声明事件的绑定。此时事件响应方法中的 sender 指的就是由哪个对象引发 的事件 2、在 panel_Green 中定义 ButtonBase.Click="xxx",将其容器内所有的 B uttonBase 类型及其子类型的事件,统一绑定到一个事件处理方法上,统一处 理。此时事件响应方法中的 sender 指的是 panel_Green 对象,而 e.Source 指的是引发 ButtonBase.Click 的某个按钮 3、路由事件的处理模型常用的有两种: 冒泡事件:由子控件位次向父容器传递,大部分的路由事件都是冒泡事件 隧道事件:由父容器位次向其子容器、控件传递,一般 PreXXX 事件属 性隧道事件 直接   
4、使用路由事件响应方法中的 e.Handled = true;意味着此事件已经被处理, 将不再传递,默认 e.Handled 的值为 false,意味着此路由事件还未处理完整, 事件将依据其模型继续向下处理(即执行其他的事件处理方法) (更多资源:http://cleopard.download.csdn.net/) (福利:http://xuemeilaile.com/) WPF 经典教程之 WPF 体系结构 http://download.csdn.net/detail/cleopard/7999393 WPF 经典教程之 WPF 应用程序管理 http://download.csdn.net/detail/cleopard/8002969 WPF 经典教程之 WPF 窗体 http://download.csdn.net/detail/cleopard/8002979 WPF 经典教程之 StackPanel、WrapPanel、DockPanel 布局 http://download.csdn.net/detail/cleopard/8002985 WPF 经典教程之 Grid、UniformGrid 布局 http://download.csdn.net/detail/cleopard/8002993 WPF 经典教程之 Canvas、InkCanvas 布局 http://download.csdn.net/detail/cleopard/8002997 WPF 经典教程之 WPF 控件模型 http://download.csdn.net/detail/cleopard/8002999 WPF 经典教程之 WPF 控件内容模型 http://download.csdn.net/detail/cleopard/8003009 等。。。。
分享到:
收藏