美图齐众专注资阳网站设计 资阳网站制作 资阳网站建设
资阳网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

WPF附加属性相关用途介绍

我们通过对WPF的深入学习,可以知道,WPF中的属性可以分为两种,一种是依赖属性而另外一种则是附加属性。我们在这里将会重点介绍WPF附加属性。#t#

WPF附加属性是允许不同的子元素为 实际在父元素中定义的属性指定***值。例如:

  1. < DockPanel> 
  2. < CheckBox DockPanel.
    Dock="Top">Hello
    < /CheckBox> 
  3. < /DockPanel> 

Dock不是CheckBox的属性,而是定义在DockPanel中的。

用代码使用:

 
 
 
  1. DockPanel myDockPanel = 
    new DockPanel();  
  2. CheckBox myCheckBox = 
    new CheckBox();  
  3. myCheckBox.Content = 
    "Hello";  
  4. myDockPanel.Children.
    Add(myCheckBox);  
  5. DockPanel.SetDock
    (myCheckBox, Dock.Top); 

如何创建WPF附加属性

1. 声明一个 DependencyProperty 类型的 public static readonly 字段,将附加属性定义为一个依赖项属性。

2. 使用 RegisterAttached 方法的返回值来定义此字段。例如:

 
 
 
  1. public class OwerClass : 
    DependencyObject  
  2. {  
  3. public static string 
    GetAttachedPropertyName
    (DependencyObject obj)  
  4. {  
  5. return (string)obj.GetValue
    (AttachedPropertyNameProperty);  
  6. }  
  7. public static void SetAttached
    PropertyName(DependencyObject 
    obj, string value)  
  8. {  
  9. obj.SetValue(AttachedProperty
    NameProperty, value);  
  10. }  
  11. public static readonly 
    DependencyProperty Attached
    PropertyNameProperty =  
  12. DependencyProperty.RegisterAttached
    ("AttachedPropertyName", 
    typeof(string), typeof(OwerClass), 
    new UIPropertyMetadata(0));  

WPF附加属性小提示:

可以利用VS2008智能提示:在class里面输入propa,然后按Tab 自动生成基本内容:)


网站栏目:WPF附加属性相关用途介绍
URL标题:http://www.zsjierui.cn/article/cccoses.html

其他资讯