如何向框架元素的可见性依赖属性添加回调?
我正在使用一个依赖属性来处理框架元素的淡入/淡出。
当动画元素的 Visibility 属性发生更改时,该属性能够通过注册回调方法来处理淡入/淡出动画。
这是由以前的编码器完成的:
UIElement.VisibilityProperty.AddOwner(typeof (FrameworkElement), new FrameworkPropertyMetadata(Visibility.Visible, VisibilityChanged, CoerceVisibility));
这里的问题是 FrameworkElement 已经是 VisibilityProperty 的所有者,因此触发了一个异常,该异常被 ExpressionBlend 捕获(幸运的是)。
为了解决这个问题,我注意到依赖属性有一个“OverwriteMetadata”方法,它允许某人覆盖给定类型的元数据,在我的例子中为 FrameworkElement。
因此,我可以使用以下内容:
UIElement.VisibilityProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(Visibility.Visible, VisibilityChanged, CoerceVisibility));
我的问题是:
覆盖 FrameworkElement 的 Visibility 元数据有多安全?如果不安全,我还有什么其他选择?
编辑:好吧,从头开始......显然覆盖元数据会触发另一个异常:“PropertyMetadata 已经为类型“FrameworkElement”注册。
如果我无法添加所有者或覆盖元数据吗?
我是否被迫创建一个派生自 FrameworkElement 的类,将其添加为 VisibilityProperty 的所有者,并使使用该属性的所有控件与派生类具有相同的类型?
I'm using a dependency property which handles the fade-in / fade-out of a Framework Element.
The property is able to handle the fade-in / fade-out animation by registering callback methods for whenever the Visibility property of the animated element has changed.
This was done by the previous coder as such:
UIElement.VisibilityProperty.AddOwner(typeof (FrameworkElement), new FrameworkPropertyMetadata(Visibility.Visible, VisibilityChanged, CoerceVisibility));
The problem here is that FrameworkElement is already an owner of the VisibilityProperty, and as such triggers an exception which was catched (luckily) by ExpressionBlend.
To counter this problem, I noticed that Dependency Properties have a "OverwriteMetadata" method, which allows someone to overwrite the metadata of a given type, in my case FrameworkElement.
As such, I could use the following instead:
UIElement.VisibilityProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(Visibility.Visible, VisibilityChanged, CoerceVisibility));
My question is:
How safe is it to overwrite FrameworkElement's Visibility's metadata ? If its unsafe, what other alternatives do I have ?
EDIT: Well, scratch that... Apparently overwriting the metadata triggers another exception: "PropertyMetadata is already registered for type 'FrameworkElement'.
How can I add the callback methods for the dependency property, if I'm unable to add an owner or overwrite the metadata ?
Am I forced to create a class which derives from FrameworkElement, add it as an owner of the VisibilityProperty, and make all controls who use that property be of the same type as the derived class ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不存在其他挂钩,则可以使用
DependencyPropertyDescriptor< /code>
添加更改的处理程序:
但是,
FrameworkElement
定义了一个IsVisibleChanged
事件 - 您可以使用它吗?If there exist no other hooks, you can use
DependencyPropertyDescriptor
to add changed handlers:However,
FrameworkElement
defines anIsVisibleChanged
event - could you use that?