Share via


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

屬性值

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

屬性

範例

若要建立水準 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 。 在這裡範例中,不會如先前範例所示設定 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針對預設值ItemsPanelTemplate指定VirtualizingStackPanel 。 針對 MenuItem預設值會使用 WrapPanel。 針對 StatusBar預設值會使用 DockPanel

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

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

相依性屬性資訊

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

適用於