构建复合自定义控件WPF的最佳实践 [英] Best practices of building composite custom controls WPF

查看:92
本文介绍了构建复合自定义控件WPF的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能告诉我有关主演的信息吗?
例如,我需要创建新的自定义控件,该控件必须从datagrid和工具栏派生。我希望该新控件公开/传播基本控件的属性,以便可以轻松访问它们。我知道的唯一方法是派生一堂课。然后,后代会自动获取祖先的所有属性。但是禁止在C#中进行多次派生,因此我不知道如何公开第二个控件的属性和其他行为...还需要对此类自定义控件进行样式化和模板化。

Can You tell me about subj? For example I need to create new custom control that must be derived from datagrid and toolbar. And I want that new control to expose/propagate properties of base controls in order they to be accessed easily. The only way I know is deriving a class. Then descendant automatically gets all properties of ancestor. But multiple deriving in C# is prohibited, so I don't know how to expose properties and other behavior of second control... Styling and templating of such custom control is also needed.

谢谢!

推荐答案

在WPF中,继承是 out。 ; O)实际上,在WPF之前已经很复杂了,但是有了WPF,您可以获得各种非常强大的选择。

In WPF inheritance is "out". ;O) Actually, it was complicated before WPF already, but with WPF you get various really powerful alternatives.

对于基本控制,我最好还是使用组合,创建一些usercontrol并使其包含ToolBar和DataGrid。如果需要从外部操纵它们,则可以将它们公开为公共属性。

For the basic control I thing you'd better go with composition, create some usercontrol and make it contain a ToolBar and a DataGrid. You can then expose these as public properties, if you need to manipulate them from outside.

对于特殊功能的添加,附加属性是一种非常通用的机制。

For special feature additions, attached properties are a very versatile mechanism.

这篇关于构建复合自定义控件WPF的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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