Panel.IsItemsHost 属性

定义

获取或设置一个值,该值指示此 Panel 是由 ItemsControl 生成的 用户界面 (UI)user interface (UI) 项的容器。Gets or sets a value that indicates that this Panel is a container for 用户界面 (UI)user interface (UI) items that are generated by an ItemsControl.

public:
 property bool IsItemsHost { bool get(); void set(bool value); };
[System.ComponentModel.Bindable(false)]
public bool IsItemsHost { get; set; }
member this.IsItemsHost : bool with get, set
Public Property IsItemsHost As Boolean

属性值

Boolean

如果此 Panel 实例是项宿主,则为 true;否则为 falsetrue if this instance of Panel is an items host; otherwise, false. 默认值是 falseThe default value is false.

属性

示例

下面的示例使用 ControlTemplate 创建水平 ListBoxThe following example uses a ControlTemplate to create a horizontal ListBox. ControlTemplate中指定水平 StackPanelThe horizontal StackPanel is specified within the ControlTemplate. IsItemsHost 属性设置为在 StackPaneltrue,这指示生成的项应放置在面板中。The IsItemsHost property is set to true on the StackPanel, which indicates that the generated items should be placed in the panel. 以这种方式指定此方法时,不能使用 ControlTemplate来替换 ItemsPanel 控件的用户。When you specify it this way, the ItemsPanel cannot be replaced by the user of the control without using a ControlTemplate. 因此,仅当你知道不需要使用模板来替换面板时,才执行此操作。Therefore, do this only if you know that you do not want the panel to be replaced without the use of a template.

<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>

或者,您可以设置 "ItemsControl.ItemsPanel" 属性。Alternatively, you can set the ItemsControl.ItemsPanel property. 下面的示例在 ListBoxStyle 中设置 ItemsPanelThe following example sets the ItemsPanel in a Style of a ListBox.

<Grid.Resources>
  <Style TargetType="Separator">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Separator}">
          <Border Width="2" Height="12" Margin="4" Background="Gray"/>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style TargetType="ListBox">
    <Setter Property="ItemsPanel">
      <Setter.Value>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Horizontal"
                      VerticalAlignment="Center"
                      HorizontalAlignment="Center"/>
        </ItemsPanelTemplate>
      </Setter.Value>
    </Setter>
  </Style>
  
</Grid.Resources>

注解

不能将数据绑定到此属性。You cannot bind data to this property.

依赖项属性信息Dependency Property Information

标识符字段Identifier field IsItemsHostProperty
元数据属性设置为 trueMetadata properties set to true IsNotDataBindable

适用于

另请参阅