GroupStyle 类

定义

定义希望组查看每个级别的方式。

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
[<System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)>]
type GroupStyle = class
    interface INotifyPropertyChanged
Public Class GroupStyle
Implements INotifyPropertyChanged
继承
GroupStyle
属性
实现

示例

以下示例演示 ItemsControl 绑定到 XmlDataProvider 的 ,以及包含用于添加和删除分组的逻辑的代码隐藏内容。 选中“检查”框时,将按 Type 属性对 的内容ItemsControl进行分组。

每个组的类型均为 CollectionViewGroupGroupStyleHeaderTemplate指定 ,以便它显示为显示TextBlockName每个组的 。 在这种情况下, NameWorkHome

<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 类的新实例。

字段

DefaultGroupPanel

标识默认 ItemsPanelTemplate(创建用于控制项布局的面板)。

属性

AlternationCount

获取或设置交替 GroupItem 对象的数量。

ContainerStyle

获取或设置应用于为每个项生成的 GroupItem 的样式。

ContainerStyleSelector

使应用程序编写器能够为要应用到每个生成的 GroupItem 的样式提供自定义选择逻辑。

Default

获取组的默认样式。

HeaderStringFormat

获取或设置一个撰写字符串,该字符串指定如果标题显示为字符串,应如何设置该标题的格式。

HeaderTemplate

获取或设置用于显示组标题的模板。

HeaderTemplateSelector

使应用程序编写器能够为用于显示组标题的模板提供自定义选择逻辑。

HidesIfEmpty

获取或设置一个值,该值指示是否应该显示与空组相对应的项。

Panel

获取或设置可创建用于控制项布局的面板的模板。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
OnPropertyChanged(PropertyChangedEventArgs)

使用提供的自变量引发 PropertyChanged 事件。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

事件

PropertyChanged

在属性值更改时发生。

显式接口实现

INotifyPropertyChanged.PropertyChanged

在属性值更改时发生。

适用于

另请参阅