Dependency Property Overriew
来源:互联网 发布:org.apache.http不存在 编辑:程序博客网 时间:2024/05/16 07:35
1)The difference between CLR properties and Dependency Properties
DP支持:Change Notification, Property Value Inheritance, Support for Multiple providers.
结论: DPs are more than just simple CLR properties.
2)如何创建一个Dependency Property
public
class Button : ButtonBase
{
public
static readonly DependencyProperty IsDefaultProperty;
public
Button() {DependencyProperty.Register();...}
public
bool IsDefault {
get {return GetValue(Button.IsDefaultPeroperty);}
set {SetValue(Button.IsDefaultPeroperty, value); } }
}
"Although the XAML compiler depends on the wrapper at compile time, at run time WPF calls the underlying GetValue and SetValue methods directly! Therefore, to maintain parity between setting a property in XAML and procedural code, it's crucial that property wrappers DO NOT contain any logic in addition to the GetValue/SetValue calls. If you want to add custom logic, that what the registered callbacks are for. All of WPF's built in property wrapper abide by this rule, so this warning is for anyone writing a custom class with its own dependency properties."
==尽管XAML编译器需要WapperProperty,但是运行期间WPF直接调用GetValue/SetValue。所以为了保持一致性,需要在.NET Wrapper Property不要包含任何逻辑.
3)Change Notification
当DP发生变化是,会自动触发一系列的活动。比如:Property Trigger。通过配置的方式直接实现事件,无需写EventHandler的C# Code.
<Trigger Property="IsMouseOver" Value=“True”>
<Setter Property="Foreground", Value="Blue" />
</Trigger>
4)
含义:属性值会Flowing down the element tree from top to down.
还有一种特殊形式的Property 是Attached property,允许把Property attached 任意个对象上面。
比如:TextElement.FontSize;比如StackPanel没有FontSize属性,没有办法inheritence to child property,这时可以用Attached Property。
XAML Code:
5)Support Multiple Providers.
Thanks for Change Notification,可以通过4个流程计算出最终的Dependency Propety Value.
Determine Base Value--------------> Evaluate (if is Expression)------------------->Apply Animations----------->Coerce---->Validate.
其中Determine Base Value,最高到最低的优先顺序为:
Local Value->Style Trigger->Template Trigger->Style Setters->Theme Style triggers->Theme style setters->Property Value inheritance->Defalut Value.
根据这个就解释了Status Bar的FontSize为什么没有从Stack Panel得到值,Style Setters.
DepedencyProperty(一般放在Custom Control), Default Value:
确保类型一致,
DependencyProperty.Register("Value", typeof (decimal), typeof (MainWindow), new PropertyMetadata(10)
会发生异常!!!
this.DataContext = this/ViewModel;
<TextBlock Text="{Binding Path=Value}" Grid.Row="0"/>
public decimal Value
{
get
{
return Convert.ToDecimal(base.GetValue(ValueProperty));
}
set
{
//不能有任何的逻辑
base.SetValue(ValueProperty,value);
}
}
public static DependencyProperty
ValueProperty = DependencyProperty.Register("Value", typeof (decimal), typeof (MainWindow),
new PropertyMetadata(Convert.ToDecimal(10)));
private void Button_Click(object sender, RoutedEventArgs e)
{
Value += 1;
}
- Dependency Property Overriew
- Dependency Property
- Dependency property in WPF
- Dependency Property 依赖属性
- 依赖属性Dependency Property
- MVC Overriew
- Silverlight:Dependency Property(依赖属性)
- WPF中的Dependency Property(1)
- Silverlight:Dependency Property(依赖属性)学习笔记
- Silverlight:Dependency Property(依赖属性)学习笔记
- Unsatisfied dependency expressed through bean property 'dataSource'
- WPF中的依赖属性(Dependency Property)
- (WPF学习记录)第八章 Dependency Property
- WPF的Dependency Property System——出自《IT168》
- 依赖属性 dependency property WPF,为什么要用DDP?
- #122 – Validating a Dependency Property(验证依赖属性)
- Dependency
- 为什么在自定义控件(custom control)中定义的dependency property一定是readonly static?
- Compiling Hydrax 0.5.1 with Ogre 1.7
- .net pathfiner(寻路算法)
- rcp 首选项中常规|更新|文本编辑器等自定义
- xml的开发和编辑工具
- js字符串转换成数字
- Dependency Property Overriew
- find命令详解
- vc 运行exe文件的方法
- linux系统管理
- python查找中文字符
- 电脑专业英语1500词
- JavaScript 树
- OpenGL中的多视口
- 最全的windows xp找回密码技巧