更新
This commit is contained in:
34
WPF/AttachedProperty.md
Normal file
34
WPF/AttachedProperty.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# 释义
|
||||
|
||||
本不属于某个对象的属性,在被需要的时候后来附加;在特定环境下,对象才具有的属性,如当一个控件在Grid中时,会附加Grid.Column属性等
|
||||
|
||||
本质上是依赖属性
|
||||
用clr属性包装时不同,是采用两个方法
|
||||
|
||||
```csharp
|
||||
public static int GetMyProperty(DependencyObject obj)
|
||||
{
|
||||
return (int)obj.GetValue(MyPropertyProperty);
|
||||
}
|
||||
|
||||
public static void SetMyProperty(DependencyObject obj, int value)
|
||||
{
|
||||
obj.SetValue(MyPropertyProperty, value);
|
||||
}
|
||||
|
||||
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
|
||||
public static readonly DependencyProperty MyPropertyProperty =
|
||||
DependencyProperty.RegisterAttached("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0));
|
||||
```
|
||||
|
||||
把年级附加属性附加给人
|
||||
|
||||
```csharp
|
||||
class Human:DependencyObject
|
||||
{
|
||||
|
||||
}
|
||||
Human human=new Human()
|
||||
School.SetMyProperty(human,6)//设置属性
|
||||
int grade=School.GetMyProperty(human)//得到属性值
|
||||
```
|
||||
Reference in New Issue
Block a user