ItemsPanelTemplate 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定 ItemsPresenter 为 ItemsControl 的项的布局创建的面板。
public ref class ItemsPanelTemplate : System::Windows::FrameworkTemplate
public class ItemsPanelTemplate : System.Windows.FrameworkTemplate
type ItemsPanelTemplate = class
inherit FrameworkTemplate
Public Class ItemsPanelTemplate
Inherits FrameworkTemplate
- 继承
示例
若要创建水平 ListBox,可以创建一个指定水平 StackPanel 的模板,并将其设置为 ItemsPanel 属性。 下面的示例演示了创建 ListBoxStyle 水平 ListBox的 。
<Style TargetType="ListBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
以下示例使用 ControlTemplate 创建具有圆角的水平 ListBox 。 请注意,在此示例中,在 中ControlTemplate指定水平StackPanel,而不是像上一示例中那样设置 ItemsPanel 属性。 请注意, 上的 IsItemsHost 属性设置为 true
StackPanel,指示生成的项应进入面板中。 以这种方式指定时,如果不使用 ControlTemplate,ItemsPanel则控件的用户无法替换 。 因此,仅当知道不希望在不使用模板的情况下替换面板时,才执行此操作。
<Style TargetType="ListBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<Border CornerRadius="5" Background="{TemplateBinding ListBox.Background}">
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<StackPanel Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Center"
IsItemsHost="True"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
或者,可以执行以下操作来实现相同的结果。 在这种情况下, ItemsPresenter 会根据 指定的 ItemsPanelTemplate内容为项的布局创建面板。
<Style TargetType="{x:Type ListBox}">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Border CornerRadius="5"
Background="{TemplateBinding ListBox.Background}">
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<ItemsPresenter/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
有关如何在 上使用 ItemsControl不同样式设置和模板化属性的示例,请参阅 数据模板化概述。
注解
指定 ItemsPanelTemplate 用于项布局的面板。 GroupStylePanel具有类型ItemsPanelTemplate为 的属性。 ItemsControlItemsPanel类型的属性的类型为 ItemsPanelTemplate。
每个类型都有 ItemsControl 一个默认 ItemsPanelTemplate的 。 ItemsControl对于 类,默认值ItemsPanel为指定 ItemsPanelTemplate 的 StackPanel。 ListBox对于 ,默认值使用 VirtualizingStackPanel。 对于 MenuItem,默认值使用 WrapPanel。 对于 StatusBar,默认值使用 DockPanel。
构造函数
ItemsPanelTemplate() |
初始化 ItemsPanelTemplate 类的实例。 |
ItemsPanelTemplate(FrameworkElementFactory) |
用指定的模板初始化 ItemsPanelTemplate 类的实例。 |
属性
Dispatcher |
获取与此 Dispatcher 关联的 DispatcherObject。 (继承自 DispatcherObject) |
HasContent |
获取一个值,该值指示此模板是否具有优化内容。 (继承自 FrameworkTemplate) |
IsSealed |
获取一个值,该值指示此对象是否处于不可变状态,如果处于该状态则无法更改对象。 (继承自 FrameworkTemplate) |
Resources |
获取或设置可在此模板范围内使用的资源集合。 (继承自 FrameworkTemplate) |
Template |
获取或设置一个引用,该引用指向当编写器定义或应用模板时用于录制或播放模板的 XAML 节点的对象。 (继承自 FrameworkTemplate) |
VisualTree |
获取或设置模板的根节点。 (继承自 FrameworkTemplate) |
方法
CheckAccess() |
确定调用线程是否可以访问此 DispatcherObject。 (继承自 DispatcherObject) |
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
FindName(String, FrameworkElement) |
查找与此模板中定义的指定名称关联的元素。 (继承自 FrameworkTemplate) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
LoadContent() |
将模板的内容加载为对象的实例,并返回该内容的根元素。 (继承自 FrameworkTemplate) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
RegisterName(String, Object) |
将新的名称/对象对注册到当前名称范围内。 (继承自 FrameworkTemplate) |
Seal() |
锁定模板,使其无法更改。 (继承自 FrameworkTemplate) |
ShouldSerializeResources(XamlDesignerSerializationManager) |
返回一个值,该值指示序列化过程是否应序列化此类的实例上的 Resources 属性值。 (继承自 FrameworkTemplate) |
ShouldSerializeVisualTree() |
返回一个值,该值指示序列化过程是否应序列化此类的实例上的 VisualTree 属性值。 (继承自 FrameworkTemplate) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |
UnregisterName(String) |
从 XAML 名称范围中移除名称/对象映射。 (继承自 FrameworkTemplate) |
ValidateTemplatedParent(FrameworkElement) |
检查模板化父级是否为非 null ItemsPresenter 对象。 |
VerifyAccess() |
强制调用线程具有此 DispatcherObject 的访问权限。 (继承自 DispatcherObject) |
显式接口实现
INameScope.FindName(String) |
返回具有提供的标识名称的对象。 (继承自 FrameworkTemplate) |
IQueryAmbient.IsAmbientPropertyAvailable(String) |
查询当前范围中是否有指定的环境属性。 (继承自 FrameworkTemplate) |
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈