Files
06-Note/WPF/AttachedProperty.md
sherlockforrest bf2ed2e31f 更新
2023-06-20 09:22:53 +08:00

35 lines
1.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 释义
本不属于某个对象的属性,在被需要的时候后来附加;在特定环境下对象才具有的属性如当一个控件在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)//得到属性值
```