ItemsControl.ItemContainerStyle 属性

定义

获取或设置应用于为每个项生成的容器元素的 Style

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

属性值

Style

应用于为每个项生成的容器元素的 Style。 默认值为 null

属性

示例

以下示例演示如何使用此属性。 请考虑以下数据绑定 ListBox

<ListBox ItemsSource="{Binding Source={StaticResource MyPhotos}}"
         Background="Silver" Width="600" Margin="10" SelectedIndex="0"/>

若要为包含数据项的元素创建样式,请创建样式 ListBoxItem ,如以下示例所示。 样式应用于定义样式的范围中的所有 ListBoxItem 元素。

<Style TargetType="ListBoxItem">
  <Setter Property="Opacity" Value="0.5" />
  <Setter Property="MaxHeight" Value="75" />
  <Style.Triggers>
    <Trigger Property="IsSelected" Value="True">
      <Trigger.Setters>
        <Setter Property="Opacity" Value="1.0" />
      </Trigger.Setters>
    </Trigger>
    <EventTrigger RoutedEvent="Mouse.MouseEnter">
      <EventTrigger.Actions>
        <BeginStoryboard>
          <Storyboard>
            <DoubleAnimation
              Duration="0:0:0.2"
              Storyboard.TargetProperty="MaxHeight"
              To="90"  />
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger.Actions>
    </EventTrigger>
    <EventTrigger RoutedEvent="Mouse.MouseLeave">
      <EventTrigger.Actions>
        <BeginStoryboard>
          <Storyboard>
            <DoubleAnimation
              Duration="0:0:1"
              Storyboard.TargetProperty="MaxHeight"  />
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger.Actions>
    </EventTrigger>
  </Style.Triggers>
</Style>

ListBoxItem 控件的 ListBox 容器元素。 因此,上述替代方法是将ItemContainerStyleListBox属性设置为定义的样式。 为此,请 ListBoxItem 为样式提供一个 x:Key 资源,使其可用:

<Style TargetType="ListBoxItem" x:Key="ContainerStyle">
  <Setter Property="Opacity" Value="0.5" />
  <Setter Property="Opacity" Value="0.5" />
  <Setter Property="MaxHeight" Value="75" />
  <Style.Triggers>
    <Trigger Property="IsSelected" Value="True">
      <Setter Property="Opacity" Value="1.0" />
    </Trigger>

接下来,将 ItemContainerStyle 属性设置为资源,如以下示例所示:

<ListBox ItemsSource="{Binding Source={StaticResource MyPhotos}}"
         ItemContainerStyle="{StaticResource ContainerStyle}" 
         Background="Silver" Width="600" Margin="10" SelectedIndex="0"/>

上述两种方案都产生相同的结果。 但是,使样式可用作资源的优点之一是可以重复使用样式。 ItemContainerStyle显式设置属性还可以提供更高的可读性。

有关完整示例,请参阅 样式和模板化示例简介

The following example is designed to illustrate the function of the different styling and templating related properties provided by the ItemsControl. ItemsControl此示例绑定到对象的集合Task。 为便于演示,本示例中的样式和模板都进行了内联声明。

<ItemsControl Margin="10"
              ItemsSource="{Binding Source={StaticResource myTodoList}}">
  <!--The ItemsControl has no default visual appearance.
      Use the Template property to specify a ControlTemplate to define
      the appearance of an ItemsControl. The ItemsPresenter uses the specified
      ItemsPanelTemplate (see below) to layout the items. If an
      ItemsPanelTemplate is not specified, the default is used. (For ItemsControl,
      the default is an ItemsPanelTemplate that specifies a StackPanel.-->
  <ItemsControl.Template>
    <ControlTemplate TargetType="ItemsControl">
      <Border BorderBrush="Aqua" BorderThickness="1" CornerRadius="15">
        <ItemsPresenter/>
      </Border>
    </ControlTemplate>
  </ItemsControl.Template>
  <!--Use the ItemsPanel property to specify an ItemsPanelTemplate
      that defines the panel that is used to hold the generated items.
      In other words, use this property if you want to affect
      how the items are laid out.-->
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <!--Use the ItemTemplate to set a DataTemplate to define
      the visualization of the data objects. This DataTemplate
      specifies that each data object appears with the Priority
      and TaskName on top of a silver ellipse.-->
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <DataTemplate.Resources>
        <Style TargetType="TextBlock">
          <Setter Property="FontSize" Value="18"/>
          <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>
      </DataTemplate.Resources>
      <Grid>
        <Ellipse Fill="Silver"/>
        <StackPanel>
          <TextBlock Margin="3,3,3,0"
                     Text="{Binding Path=Priority}"/>
          <TextBlock Margin="3,0,3,7"
                     Text="{Binding Path=TaskName}"/>
        </StackPanel>
      </Grid>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
  <!--Use the ItemContainerStyle property to specify the appearance
      of the element that contains the data. This ItemContainerStyle
      gives each item container a margin and a width. There is also
      a trigger that sets a tooltip that shows the description of
      the data object when the mouse hovers over the item container.-->
  <ItemsControl.ItemContainerStyle>
    <Style>
      <Setter Property="Control.Width" Value="100"/>
      <Setter Property="Control.Margin" Value="5"/>
      <Style.Triggers>
        <Trigger Property="Control.IsMouseOver" Value="True">
          <Setter Property="Control.ToolTip"
                  Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                          Path=Content.Description}"/>
        </Trigger>
      </Style.Triggers>
    </Style>
  </ItemsControl.ItemContainerStyle>
</ItemsControl>

下面是该示例在呈现时的屏幕快照:

ItemsControl 示例屏幕快照

此处未显示的其他两个样式相关属性 ItemsControlGroupStyleGroupStyleSelector

注解

可以使用此属性或 ItemContainerStyleSelector 属性设置样式,以影响包含数据项的元素的外观。 例如,生成的 ListBox容器是 ListBoxItem 控件;对于 ComboBox,它们是 ComboBoxItem 控件。

ItemsControl 为视觉自定义提供了极大的灵活性,并提供许多样式和模板化属性。 若要影响项的布局,请使用该 ItemsPanel 属性。 如果在控件上使用分组,可以使用 GroupStyleGroupStyleSelector 属性。 若要指定数据对象的可视化效果,请使用 ItemTemplateItemTemplateSelector 属性。 有关何时指定 ItemTemplate数据的详细信息,请参阅 数据模板化概述

XAML 属性用法

<object ItemContainerStyle="ResourceExtension StyleResourceKey"/>  

XAML 值

ResourceExtension
以下项之一: StaticResourceDynamicResource。 除非样式本身包含对潜在运行时引用(如系统资源或用户首选项)的引用, StaticResource 否则通常建议对样式的引用,以便提高性能。

StyleResourceKey
x:Key 引用作为资源请求的样式的字符串值。

依赖项属性信息

标识符字段 ItemContainerStyleProperty
元数据属性设置为 true

适用于

另请参阅