[英]UserControl that is also a Grid in WPF
I would like to create a UserControl
that will have some behavior(animated collapsing...) and some Controls in it ( Rectangle
, Ellipse
, mostly for design purposes). 我想创建一个UserControl
,它将具有一些行为(动画崩溃……)和其中的一些控件( Rectangle
, Ellipse
,主要用于设计目的)。
However I need it to be a valid Panel
like Grid
so I can put more controls in it when designing main window. 但是我需要它是一个像Grid
这样的有效Panel
以便在设计主窗口时可以在其中放置更多控件。 Controls that will be add to the new USerControl
in main window design time does not need to interact with components that are added to it when creating a UserControl
. 在创建UserControl
时,将不会在主窗口设计时添加到新USerControl
控件与添加到其中的组件进行交互。
How should I do this ? 我该怎么办? Do I have to extends Control
? 我是否必须扩展Control
? I'd prefer to extend just UserControl
. 我宁愿只扩展UserControl
。
UserControl
is a ContentControl
which has Content
property and usually displays it according on ContentTemplate
. UserControl
是一个ContentControl
,它具有Content
属性,通常根据ContentTemplate
对其进行显示。 The point I'm leading to is that UserControl
has 1 item inside of it. 我要说的是UserControl
里面有1个项目。 Of course you can add Children
property to your UserControl
and handle adding/removing of sub-items into some internal Grid
within your control but it's not what content controls are for. 当然,您可以将Children
属性添加到UserControl
并处理子项在控件中某些内部Grid
添加/删除,但这不是内容控件的用途。
To host many items you should use a subclass of Panel
, like Grid
as you mentioned. 要托管许多项目,您应该使用Panel
的子类,例如您提到的Grid
。 All panels have Children
property with a list of elements arranged on them. 所有面板均具有Children
属性,并在其上排列了元素列表。 You can inherit Grid
and place some behavior there. 您可以继承Grid
并在其中放置一些行为。 Eg you can make an extension property MyGrid.Collapsed
, set it on elements within your grid, so it can arrange their positions. 例如,您可以创建一个扩展属性MyGrid.Collapsed
,将其设置在网格中的元素上,以便它可以排列它们的位置。
Another option is to subclass ItemsControl
. 另一个选择是将ItemsControl
子类化。 Elements placed into it will be displayed according to its ItemsPanel
template. 放置在其中的元素将根据其ItemsPanel
模板显示。 This approach also gives you an advantage of ItemsSource
property which can be used to display a list of arbitrary data types by generating elements from them using ItemTemplate
property. 这种方法还为您提供了ItemsSource
属性的优点,该属性可通过使用ItemTemplate
属性从元素中生成元素来显示任意数据类型的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.