ItemsControl.ItemsPanel 屬性

定義

取得或設定範本,這個範本會定義控制專案配置的面板。

C#
[System.ComponentModel.Bindable(false)]
public System.Windows.Controls.ItemsPanelTemplate ItemsPanel { get; set; }

屬性值

ItemsPanelTemplate,定義要用於專案配置的面板。 ItemsControl 的預設值是指定 StackPanelItemsPanelTemplate

屬性

範例

若要建立水準 ListBox,您可以建立範本來指定水準 StackPanel,並將其設定為 ItemsPanel 屬性。 下列範例顯示建立水準 ListBoxListBoxStyle

XAML
<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 屬性設定為 StackPanel上的 true,表示產生的專案應該在面板中執行。 如此一來,控制件的使用者就無法取代 ItemsPanel,而不需使用 ControlTemplate。 因此,只有在您知道不想使用範本的情況下取代面板,才這麼做。

XAML
<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所指定的專案來建立專案的版面配置面板。

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

備註

針對 ListBox,預設 ItemsPanelTemplate 會指定 VirtualizingStackPanel。 針對 MenuItem,預設值會使用 WrapPanel。 針對 StatusBar,預設值會使用 DockPanel

若要影響 ItemsControl中專案的版面配置,您可以使用這個屬性來指定 ItemsPanelTemplate

ItemsControl 提供視覺自定義的絕佳彈性,並提供許多樣式和範本化屬性。 您可以使用 ItemContainerStyle 屬性或 ItemContainerStyleSelector 屬性來設定樣式,以影響包含數據項的項目外觀。 例如,針對 ListBox,產生的容器是 ListBoxItem 控件;針對 ComboBox,它們是 ComboBoxItem 控件。 如果您在控件上使用群組,您可以使用 GroupStyleGroupStyleSelector 屬性。 若要指定資料對象的視覺效果,請使用 ItemTemplateItemTemplateSelector 屬性。 如需詳細資訊,請參閱 資料範本化概觀

相依性屬性資訊

標識元欄位 ItemsPanelProperty
設定為 true 的元數據屬性 沒有

適用於

產品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9