依赖属性(Dependency Properties)进阶(一)
前端之家收集整理的这篇文章主要介绍了
依赖属性(Dependency Properties)进阶(一),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
接着依赖属性(Dependency Properties)基础进行更深一步的学习。
1) 元数据重写
@H_
301_34@
通过定义其PropertyMetadata,类可以定义依赖项属性的行为,例如,其默认值和属性系统回调。很多依赖项属性类都已经将默认元数据作为其注册过程的一部分而创建。这包含作为 WPFAPI 一部分的依赖项属性。通过其类继承继承依赖项属性的类可以重写原始的元数据,以便可以通过元数据更改的属性的特征将与任何特定于子类的要求匹配。
在依赖项属性上重写元数据的操作必须在将该属性提供给属性系统使用之前进行,也就是说,在对注册属性的对象的特定实例进行实例化之前进行。必须在将其自身提供为OverrideMetadata的forType参数的类型的静态构造内执行对OverrideMetadata的调用。如果在所有者类型的实例存在之后尝试更改元数据,这将不会引发异常,但会在属性系统中导致不一致的行为。此外,每种类型只可以重写一次元数据。以后在同一类型上重写元数据的尝试将会引发异常。
下面示例重写MyButton的Content依赖属性,将默认值改为Override:
publicclass MyButton : Button
{
static MyButton()
{
Button.ContentProperty.OverrideMetadata(typeof(MyButton),newFrameworkPropertyMetadata("Override"));
}
}
通过元数据重写,在项目使用该控件时,设计时的Context就是Override了。
2)属性值继承
@H_
301_34@
属性值继承是 Windows PresentationFoundation (WPF) 属性系统的一项功能。属性值继承使元素树中的子元素可以从父元素那里获取特定属性的值,并继承该值,就好像它是在最近的父元素中的任意位置设置的一样。父元素还可以通过属性值继承来获得其值,因此系统有可能一直递归到页面根元素。属性值继承不是属性系统的默认行为;属性必须用特定的元数据设置来建立,以便使该属性能够对子元素启动属性值继承。
如果Dependency属性在用注册的时候时指定Inherits为不可继承,这样继承就会失效。
如下,将MyButton的FontSize属性置为不可继承:
public class MyButton : Button
{
static MyButton()
{
//…
FrameworkPropertyMetadata fm = newFrameworkPropertyMetadata();
fm.Inherits = false;
Button.FontSizeProperty.OverrideMetadata(typeof(MyButton),fm);
}
}
项目中使用:
<GridGrid.Column="0" Grid.Row="1"TextElement.FontSize="20">
<Button Width="250"Content="属性值继承" Margin="73,28,73,169" />
<mycontrol:MyButtonWidth="250" Margin="73,68,129" />
</Grid>
效果(第一个button继承了Grid的字体大小,而第二个没有继承):
2.为依赖属性添加所有者类型
@H_
301_34@
将类添加为针对不同类型注册的依赖性属性的所有者。通过执行此操作,WPFXAML读取器和属性系统都可以将该类识别为属性的其他所有者。添加所有者时,也可以选择添加类来提供类型特定的元数据。使用AddOwner方法。
示例(在MyButton上添加一个标识文本(SymbolText)依赖属性,然后为其添加所有者MyLabel):
MyButton:
public class MyButton : Button
{
//…
public string SymbolText
{
get { return (string)GetValue(SymbolTextProperty); }
set { SetValue(SymbolTextProperty,value); }
}
public static readonly DependencyProperty SymbolTextProperty =
DependencyProperty.Register("SymbolText",typeof(string),typeof(MyButton),new PropertyMetadata("★"));
}
MyLabel(
这边支持元数据重写,修改了默认的标识文本
):
public class MyLabel : Label
{
//…
public static readonly DependencyProperty SymbolTextProperty = MyButton.SymbolTextProperty.AddOwner(typeof(MyLabel),new PropertyMetadata("△"));
public string SymbolText
{
get { return (string)this.GetValue(SymbolTextProperty); }
set { this.SetValue(SymbolTextProperty,value); }
}
}
样式代码就不贴了。
引用代码:
<Grid Grid.Column="0" Grid.Row="1" TextElement.FontSize="20">
<Button Width="250" Content="属性值继承" Margin="73,169" />
<mycontrol:MyButton Width="250" Margin="73,129" />
<mycontrol:MyLabel Width="250" Content="MyLabel" Margin="73,118,79"/>
</Grid>
效果:
疑问:原用ImageSource做的,但是默认值一直加不上,高手请赐教,代码如下:
/// <summary>
/// 图片
/// <para>这是依赖属性</para>
/// </summary>
public ImageSource Icon
{
get { return (ImageSource)GetValue(IconProperty); }
set { SetValue(IconProperty,value); }
}
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register("Icon",typeof(ImageSource),new PropertyMetadata(new BitmapImage(new Uri("/MyControls;component/Image/rabbit_32.png",UriKind.RelativeOrAbsolute))));