WPF自定义依赖项属性
WPF自定义Usercontrol, 需要在xaml里绑定数据, DependencyProperty是必须的.
所以定义 DependencyProperty 大概是这样







































然而根据WPF规范,依赖项属性同时又是附加属性(AttachedProperty) , 现在让属性更符合规范一些
所以上面的代码就改为


public static readonly DependencyProperty FrontContentProperty
= DependencyProperty.RegisterAttached(
"FrontContent",
typeof(UIElement),
typeof(myUserControl),
new PropertyMetadata(
null,
new PropertyChangedCallback(FrontContentChangedCallback))
);
//xaml内引用名为 "XXXX"
//则依赖项属性名必须为 XXXXProperty
//且必须有2个静态方法SetXXXX()和GetXXXX()
public static void SetFrontContent(myUserControltarget, UIElement value)
{
target.SetValue(FrontContentProperty, value);
}
public static UIElement GetFrontContent(myUserControltarget)
{
return (UIElement)target.GetValue(FrontContentProperty);
}