GroupStyle 類別

定義

定義您要群組查看各個層級的方式。Defines how you want the group to look at each level.

public ref class GroupStyle : System::ComponentModel::INotifyPropertyChanged
[System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)]
public class GroupStyle : System.ComponentModel.INotifyPropertyChanged
type GroupStyle = class
    interface INotifyPropertyChanged
Public Class GroupStyle
Implements INotifyPropertyChanged
繼承
GroupStyle
屬性
實作

範例

下列範例會顯示系結至 XmlDataProviderItemsControl,以及包含新增和移除群組之邏輯的程式碼後置內容。The following examples show an ItemsControl that is bound to an XmlDataProvider and the code-behind content that contains the logic to add and remove grouping. 核取此核取方塊時,ItemsControl 的內容會依照 Type 屬性分組。When the check box is checked, the content of the ItemsControl is grouped by the Type attribute.

每個群組都是 CollectionViewGroup的類型。Each group is of type CollectionViewGroup. 系統會指定 GroupStyle HeaderTemplate,使其顯示為顯示每個群組 NameTextBlockThe GroupStyle HeaderTemplate is specified so that it appears as a TextBlock that displays the Name of each the group. 在此情況下,NameWorkHomeIn this case, the Name is either Work or Home.

<Window x:Class="GroupingSample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Grouping Sample"
    Width="220" Height="550">
  <StackPanel>

    <StackPanel.Resources>
      <XmlDataProvider x:Key="myTasks" XPath="Tasks/Task">
        <x:XData>
          <Tasks xmlns="">
            <Task Name="Groceries" Priority="2" Type="Home">
              <Description>Pick up Groceries and Detergent</Description>
            </Task>
            <Task Name="Laundry" Priority="2" Type="Home">
              <Description>Do Laundry</Description>
            </Task>
            <Task Name="Email" Priority="1" Type="Work">
              <Description>Email Clients</Description>
            </Task>
            <Task Name="Clean" Priority="3" Type="Work">
              <Description>Clean my office</Description>
            </Task>
            <Task Name="Dinner" Priority="1" Type="Home">
              <Description>Get ready for family reunion</Description>
            </Task>
            <Task Name="Proposals" Priority="2" Type="Work">
              <Description>Review new budget proposals</Description>
            </Task>
          </Tasks>
        </x:XData>
      </XmlDataProvider>
    </StackPanel.Resources>

    <TextBlock Margin="12,5,5,0" FontSize="20" Text="My Task List"/>
    <CheckBox Margin="10,5,5,10" Checked="AddGrouping"
              Unchecked="RemoveGrouping">Group by task type</CheckBox>
    <ItemsControl Margin="10" Name="myItemsControl"
                  ItemsSource="{Binding Source={StaticResource myTasks}}">
      <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 XPath=@Name}"/>
              <TextBlock Margin="3,0,3,7"
                         Text="{Binding XPath=@Priority}"/>
            </StackPanel>
          </Grid>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
      <ItemsControl.ItemContainerStyle>
        <Style>
          <Setter Property="Control.Width" Value="100"/>
          <Setter Property="Control.Margin" Value="5"/>
        </Style>
      </ItemsControl.ItemContainerStyle>
      <ItemsControl.GroupStyle>
        <GroupStyle>
          <GroupStyle.HeaderTemplate>
            <DataTemplate>
              <TextBlock FontWeight="Bold" FontSize="15"
                         Text="{Binding Path=Name}"/>
            </DataTemplate>
          </GroupStyle.HeaderTemplate>
        </GroupStyle>
      </ItemsControl.GroupStyle>
    </ItemsControl>
  </StackPanel>
</Window>
using System;
using System.Windows;
using System.Windows.Data;

namespace GroupingSample
{
    public partial class Window1 : System.Windows.Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        CollectionView myView;
        private void AddGrouping(object sender, RoutedEventArgs e)
        {
            myView = (CollectionView)CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource);
            if (myView.CanGroup == true)
            {
                PropertyGroupDescription groupDescription
                    = new PropertyGroupDescription("@Type");
                myView.GroupDescriptions.Add(groupDescription);
            }
            else
            {
                return;
            }
        }

        private void RemoveGrouping(object sender, RoutedEventArgs e)
        {
            myView = (CollectionView)CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource);
            myView.GroupDescriptions.Clear();
        }
    }
}

Imports System.Windows
Imports System.Windows.Data

Namespace GroupingSample
    Partial Public Class Window1
        Inherits System.Windows.Window

        Public Sub New()
            InitializeComponent()
        End Sub

        Private myView As CollectionView
        Private Sub AddGrouping(ByVal sender As Object, ByVal e As RoutedEventArgs)
            myView = CType(CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource), CollectionView)
            If myView.CanGroup = True Then
                Dim groupDescription As New PropertyGroupDescription("@Type")
                myView.GroupDescriptions.Add(groupDescription)
            Else
                Return
            End If
        End Sub

        Private Sub RemoveGrouping(ByVal sender As Object, ByVal e As RoutedEventArgs)
            myView = CType(CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource), CollectionView)
            myView.GroupDescriptions.Clear()
        End Sub
    End Class
End Namespace

建構函式

GroupStyle()

初始化 GroupStyle 類別的新執行個體。Initializes a new instance of the GroupStyle class.

欄位

DefaultGroupPanel

識別預設的 ItemsPanelTemplate,它可建立用來配置項目的面板。Identifies the default ItemsPanelTemplate that creates the panel used to layout the items.

屬性

AlternationCount

取得或設定替代 GroupItem 物件的數目。Gets or sets the number of alternating GroupItem objects.

ContainerStyle

取得或設定套用至為每個項目所產生之 GroupItem 的樣式。Gets or sets the style that is applied to the GroupItem generated for each item.

ContainerStyleSelector

可讓應用程式撰寫者提供自訂的樣式選取邏輯,以套用至每個產生的 GroupItemEnables the application writer to provide custom selection logic for a style to apply to each generated GroupItem.

Default

取得群組的預設樣式。Gets the default style of the group.

HeaderStringFormat

取得或設定複合字串,這個字串表示如何格式化顯示成字串的標頭。Gets or sets a composite string that specifies how to format the header if it is displayed as a string.

HeaderTemplate

取得或設定用來顯示群組標頭的範本。Gets or sets the template that is used to display the group header.

HeaderTemplateSelector

可讓應用程式撰寫者提供自訂的範本選取邏輯,以用來顯示群組標頭。Enables the application writer to provide custom selection logic for a template that is used to display the group header.

HidesIfEmpty

取得或設定值,這個值表示是否應該顯示對應到空群組的項目。Gets or sets a value that indicates whether items corresponding to empty groups should be displayed.

Panel

取得或設定範本,這個範本建立用來配置項目的面板。Gets or sets a template that creates the panel used to layout the items.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
OnPropertyChanged(PropertyChangedEventArgs)

使用提供的引數,引發 PropertyChanged 事件。Raises the PropertyChanged event using the provided arguments.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

事件

PropertyChanged

當屬性值變更時發生。Occurs when a property value changes.

明確介面實作

INotifyPropertyChanged.PropertyChanged

當屬性值變更時發生。Occurs when a property value changes.

適用於

另請參閱