WPF控件继承

编程入门 行业动态 更新时间:2024-10-15 18:24:15
本文介绍了WPF控件继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已阅读以下博客句子:

WPF的第一条规则: 避免控制继承。

The first rule of WPF: Avoid control inheritance.

我在其他地方也见过类似的事情。 但是,我无法理解逻辑。

I've seen similar things in other places as well. However, I fail to understand the logic.

此外,我在StackOverflow中看到了涉及继承WPF控件的建议(请参阅我的上一个问题,例如)。

Moreover, I see suggestions here in StackOverflow that involves inheriting WPF controls (see the replies to my previous question for example).

我想了解为什么(以及如果)应该避免控制继承。

I would like to understand why (and if) control inheritance should be avoided.

推荐答案

WPF控件看起来很 。换句话说,他们的外表与他们的行为是分开的。通常,您实际上想要自定义外观 - 而不是行为。与Winforms的世界不同,这不需要您继承新控件并覆盖渲染逻辑。相反,你在控件上设置了一些属性,如果你无法从其他属性中获得所需的外观,则可以覆盖模板本身。

WPF controls are "lookless". In other words, their look is separated from their behavior. More often than not, you actually want to customize the look - not the behavior. Unlike the world of Winforms, this does not require you to inherit a new control and override rendering logic. Instead, you set some properties on the control, resorting to overriding the template itself if you can't get the look you want from other properties.

注意避免就是这个意思。如果可以,请避免继承。如果您需要修改行为,继承可能是最佳选择。

Note that "avoid" means just that. Avoid inheritance if you can. In cases where you need to modify behavior, inheritance may well be the best option.

更多推荐

WPF控件继承

本文发布于:2023-11-27 23:15:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!