开发者

How to add callbacks to Framework Element's Visibility Dependency Property?

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 FrameworkProperty开发者_高级运维Metadata(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 ?


If there exist no other hooks, you can use DependencyPropertyDescriptor to add changed handlers:

var desc = DependencyPropertyDescriptor.FromProperty(FrameworkElement.VisibilityProperty, typeof(FrameworkElement));
desc.AddValueChanged(this.OnVisibilityChanged);

However, FrameworkElement defines an IsVisibleChanged event - could you use that?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜