仅在实现 Padding 属性的元素上设置 Padding

发布于 2024-08-30 01:24:32 字数 729 浏览 10 评论 0原文

我正在使用 System.Windows.Documents 命名空间中的类,尝试编写一些通用代码,这些代码将有条件地设置某些依赖项属性的值,具体取决于这些属性是否存在于给定类中。

例如,以下方法将任意值分配给传递的 FrameworkContentElementPadding 属性:

void SetElementPadding(FrameworkContentElement element)
{
    element.SetValue(Block.PaddingProperty, new Thickness(155d));
}

但是,并非 FrameworkContentElement 的所有具体实现都具有Padding 属性(Paragraph 可以,但 Span 没有),因此我希望实现此属性的类型的属性分配能够成功,并且会默默地进行忽略不支持的类型。

但对于 FrameworkContentElement 的所有派生实例,上述属性分配似乎都会成功,无论它们是否实现 Padding 属性。我做出这个假设是因为我总是能够读回分配的值。

我认为我分配属性值的方式存在一些缺陷。我应该怎么做才能确保给定的依赖属性分配被未实现该属性的类忽略?

非常感谢您的建议。

蒂姆

I am working with the classes in the System.Windows.Documents namespace, trying to write some generic code that will conditionally set the value of certain dependency properties, depending on whether these properties exist on a given class.

For example, the following method assigns an arbitrary value to the Padding property of the passed FrameworkContentElement:

void SetElementPadding(FrameworkContentElement element)
{
    element.SetValue(Block.PaddingProperty, new Thickness(155d));
}

However, not all concrete implementations of FrameworkContentElement have a Padding property (Paragraph does but Span does not) so I would expect the property assignment to succeed for types that implement this property and to be silently ignored for types that do not.

But it seems that the above property assignment succeeds for instances of all derivatives of FrameworkContentElement, regardless of whether they implement the Padding property. I make this assumption because I have always been able to read back the assigned value.

I assume there is some flaw in the way I am assigning property values. What should I do to ensure that a given dependency property assignment is ignored by classes that do not implement that property?

Many thanks for your advice.

Tim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

清泪尽 2024-09-06 01:24:32

所有从 Block 派生的类都具有 Padding 属性。您可以使用以下修改:

void SetElementPadding(FrameworkContentElement element)
{
    var block = element as Block;
    if (block == null) return;

    block.Padding = new Thickness(155d);
}

即使没有此修改,一切仍然对您有用,因为您想要的只是让不支持它的类忽略 Padding。这正是将会发生的情况。事实上,您可以在不支持 Padding 的实例上读出 Padding 依赖属性的值,这可能是设计使然,但您不应该关心。区块和衍生品将尊重该价值,而所有其他人将忽略它。

All classes that derive from Block have the Padding property. You may use the following modification:

void SetElementPadding(FrameworkContentElement element)
{
    var block = element as Block;
    if (block == null) return;

    block.Padding = new Thickness(155d);
}

Even without this modification everything would still work for you because all you want is for Padding to be ignored by classes that do not support it. This is exactly what would happen. The fact that you can read out the value of a Padding dependency property on an instance that does not support it is probably by design but you shouldn't care. Block and derivatives would honor the value and all others would ignore it.

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