WPF控件继承 [英] WPF control inheritance

查看:132
本文介绍了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控件继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆