如何向框架元素的可见性依赖属性添加回调?

发布于 2024-12-09 09:36:25 字数 1058 浏览 1 评论 0原文

我正在使用一个依赖属性来处理框架元素的淡入/淡出。

当动画元素的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2024-12-16 09:36:25

如果不存在其他挂钩,则可以使用 DependencyPropertyDescriptor< /code>添加更改的处理程序:

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

但是,FrameworkElement 定义了一个 IsVisibleChanged 事件 - 您可以使用它吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文