ItemsControl,ItemsPanel和ItemsPresenter(Silverlight,XAML) [英] ItemsControl, ItemsPanel and ItemsPresenter (Silverlight, XAML)
问题描述
我对这三个术语完全感到困惑,什么时候使用哪个?是什么关系,它们是哪个控件的子代?
I'm utterly confused by these 3 terms, when to use which? What's the relationship and they are children of which controls?
说这是树是否正确:
ItemsControl> ItemsPresenter> ItemsPanel
ItemsControl > ItemsPresenter > ItemsPanel
推荐答案
ItemsControl
从概念上讲是一个容纳项目的控件.尝试简单地将此控件视为零个或多个对象的持有者.
ItemsControl
is conceptually a control that houses items. Try to simply think of this control as a holder for zero or more objects.
ItemsPresenter
很难解释,但这是ItemsControl
模板的一部分,该模板将定义项目在其中的放置位置.您的ItemsControl模板可以是任何您喜欢的东西,比如说一个带有一些漂亮图片的网格,在该模板内部,您可以将ItemsPresenter
放置在您想要放置项目的任何位置,例如在网格中间. (此示例摘自 msdn 并进行了简化为了便于阅读)
ItemsPresenter
is a bit tougher to explain, but this is part of the ItemsControl
template that will define where the items are placed within it. Your ItemsControl's template can be anything you like, say a Grid with some pretty pictures around it, inside this template, you would place the ItemsPresenter
where ever you want your items to be, say right in the middle of your grid. (this example is taken from msdn and simplified for ease of reading)
<Style TargetType="HeaderedItemsControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type HeaderedItemsControl}">
<Grid>
<Rectangle Stroke="Black" Fill="Red"/>
<ItemsPresenter Margin="2,0,0,0"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
ItemsPanel
是控制ItemsControl中项目布局的面板(或容器).因此,如果您希望已添加到ItemsControl的项目以水平方式显示,则您的项目面板可以只是一个其方向"属性设置为水平"的StackPanel.
The ItemsPanel
is the panel (or container) that controls the layout of the items in your ItemsControl. So if you want your items that you have added to your ItemsControl to display in a horizotal way, then yor items panel could simply be a StackPanel with its Orientation property set to Horizontal.
这一切都说得通吗?
这篇关于ItemsControl,ItemsPanel和ItemsPresenter(Silverlight,XAML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!