WPF依赖属性的当前值(Current Value),基值(Base Value)和本地值(Local Value)
|
依赖属性的当前值(Current Value),基值(Base Value)和本地值(Local Value)是MSDN常出现的三个词,这些属性和依赖属性的优先级设置有关。 如下表: 这里列出一部分以依赖属性值优先级的情况,完整列表请参考MSDN:http://msdn.microsoft.com/zh-cn/library/ms743230.aspx 当前值: 是最外层的属性值,可能经过一些列的处理或影响而得到的最终值。 基值: 当依赖属性没有经过系统强制转换,没有经过动画处理影响的值,就是上表中在第三项之上,但没有被一和二项所影响的值。 本地值: 这个是比较直观设置的值,是通过依赖属性SetValue方法或者资源或绑定设置的值。 那么如何得到这些值呢? 当前值:通过依赖属性的GetValue方法 基值:通过IAnimatable接口的GetAnimationBaseValue方法 本地值:通过依赖属性的ReadLocalValue方法,如果没有本地值则返回DependencyProperty.UnsetValue字段 假设有两个按钮,名称分别是btn1和btn2,程序运行后,按钮的宽度(Width依赖属性)都会被DoubleAnimation动画从50变为100,两个按钮的唯一区别是btn1的Width是直接设置的。btn2的Width是通过样式的Setter设置的。 那么当动画结束后,两个按钮的Width依赖属性的具体值是这样的: 由于动画设置了依赖属性,所以当前值是100. 而在动画前,Width都是50,所以基值都是50 而btn1直接设置了Width,就是调用了依赖属性的SetValue,所以本地值被设置。 而btn2通过样式的Setter设置Width,属于上表中的第四项,本地值没有被设置。 通过程序可以实际测试一下: 获取属性的代码参考: object[] GetValues(object obj,DependencyProperty pro)
{
if (obj is DependencyObject == false || obj is IAnimatable == false)
throw new ArgumentException();
object[] re = new object[3];
var dobj = (DependencyObject)obj;
var ianm = (IAnimatable)obj;
re[0] = dobj.GetValue(pro);
re[1] = ianm.GetAnimationBaseValue(pro);
re[2] = dobj.ReadLocalValue(pro);
return re;
}
string GetValuesString(object obj,DependencyProperty pro,string name)
{
var objs = GetValues(obj,pro);
return String.Format("{3}n当前值:{0}n基值:{1}n本地值:{2}nn",objs[0],objs[1],objs[2],name);
}
<Button Width="50" Height="30" Name="btn1" Content="1" >
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Width"
To="100" Duration="0:0:0.5"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
<Button Height="30" Name="btn2" Content="2" >
<Button.Style>
<Style TargetType="Button">
<Setter Property="Width" Value="50"/>
</Style>
</Button.Style>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Width"
To="100" Duration="0:0:0.5"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
