ItemsControl.ItemsPanel 屬性

定義

取得或設定樣板,這個樣板會定義控制項目配置的面板。

public:
 property System::Windows::Controls::ItemsPanelTemplate ^ ItemsPanel { System::Windows::Controls::ItemsPanelTemplate ^ get(); void set(System::Windows::Controls::ItemsPanelTemplate ^ value); };
[System.ComponentModel.Bindable(false)]
public System.Windows.Controls.ItemsPanelTemplate ItemsPanel { get; set; }
[<System.ComponentModel.Bindable(false)>]
member this.ItemsPanel : System.Windows.Controls.ItemsPanelTemplate with get, set
Public Property ItemsPanel As ItemsPanelTemplate

屬性值

ItemsPanelTemplate

定義項目配置要使用之面板的 ItemsPanelTemplateItemsControl 的預設值為指定 StackPanelItemsPanelTemplate

屬性

範例

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

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

下列範例會使用 ControlTemplate 建立具有圓角的水準 ListBox 。 在此範例中,不會如先前範例所示設定 ItemsPanel 屬性,而是在 ControlTemplate 內指定水準 StackPanel 。 屬性 IsItemsHost 會在 上 StackPanel 設定為 true ,表示產生的專案應該在面板中執行。 當您以這種方式指定時, ItemsPanel 控制項的使用者 ControlTemplate 無法使用 來取代 。 因此,只有在您知道不想使用範本的情況下取代面板,才這麼做。

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

備註

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

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

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

相依性屬性資訊

識別碼欄位 ItemsPanelProperty
設定為 的中繼資料屬性 true

適用於

另請參閱