我已阅读以下博客句子:
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控件继承
发布评论