WPF:包含控件的自定义UserControl与继承控件 [英] WPF: Custom UserControl that includes a control versus inherits a control

查看:434
本文介绍了WPF:包含控件的自定义UserControl与继承控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个专用的ListBox/View(类型不相关)的UserControl.现在,我面临的一种选择是将类型保留为UserControl还是继承List控件.

I'm creating a UserControl that's a specialized ListBox/View (type not relevant). Now I'm faced with the option to either keep the type as UserControl or Inherit the List control.

1)如果我将其保留为UserControl,则其中有一个List控件,然后必须为ItemsSource等创建DP.

1) If I keep it as a UserControl I have a List control inside it and then I have to create a DP for ItemsSource and so on.

2)让它继承List控件,从而自动显示ItemsSource属性.

2) Let it inherit List control and thus it automatically exposes ItemsSource property.

是可以接受的方式,还是会成为某种代码恐怖的手段.会发生什么.

Is either way acceptable or will it become some Code Horror. What is expected.

是否可能有一个我不知道的选项3?

Is there maybe a option 3 I'm not aware off?

推荐答案

恐怕没有一个正确的答案.相对优点是:

There is no single right answer I'm afraid. The relative merits are:

#1在UserControl

专业人士

  • 设计此控件比较容易,即VisualStudio很好地支持UserControls.
  • 例如,您可以限制要扩展"的控件的接口,如果要取消ListView的某些功能,可以通过不公开它来实现.
  • It is easier to design this control, i.e. VisualStudio supports UserControls quite nicely
  • You can restrict the interface of the control you are 'extending' for example, if you want to suppress some functionality of the ListView, you can do this by simply not exposing it.

缺点

  • 您必须通过创建自己的ItemsSourceItemTemplate属性等来手动公开ListView的功能.但是,您可以根据需要将ListView公开为UserControl的属性.
  • You have to manually expose the functionality of the ListView, by creating your own ItemsSource, ItemTemplate properties etc ... You can however expose the ListView as a property of your UserControl if you like.

#2从ListView

专业人士

  • 如果要替换现有的ListView实例,则立即替换插件.
  • 您不必镜像ListView的属性.
  • Gives an immediate plug-in replacement if you are replacing existing ListView instances.
  • You do not have to mirror the properties of the ListView.

缺点

  • 如果要添加其他控件,则必须创建一个新模板.这可能很复杂.
  • 您不能轻易取消ListView的功能.
  • If you are adding additional controls, you have to create a new template. This can be complex.
  • You cannot easily suppress functionality of the ListView.

因此,这实际上取决于您要实现的目标.就个人而言,如果您想显着更改API,例如,将ListView专门用于非常特定的用途,我会选择(1).如果您要创建一个高度通用的扩展程序,我会选择(2).

So, it really depends on what you want to achieve. Personally I would go for (1) if you want to significantly change the API, for example specialising the ListView for a very specific purpose. I would go for (2) if you are creating a highly generic extension.

这篇关于WPF:包含控件的自定义UserControl与继承控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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