PropertyOrder 类

更新:2007 年 11 月

用于设置属性在某个类别或子属性列表中的显示顺序。

命名空间:  Microsoft.Windows.Design.PropertyEditing
程序集:  Microsoft.Windows.Design(在 Microsoft.Windows.Design.dll 中)

语法

声明
Public NotInheritable Class PropertyOrder _
    Inherits OrderToken
用法
Dim instance As PropertyOrder
public sealed class PropertyOrder : OrderToken
public ref class PropertyOrder sealed : public OrderToken
public final class PropertyOrder extends OrderToken

备注

创建私有 PropertyOrder 实例,以便在“属性”窗口中将一组特定的属性组合在一起。

PropertyOrder 类用于控制属性(包括根属性和子属性)的排序。根属性首先按类别排序,然后按字母顺序排序,最后按 PropertyOrder 排序。子属性先按 PropertyOrder 排序,然后按字母顺序排序。

说明:

此行为不同于 Windows 窗体设计器的行为,Windows 窗体设计器使用 GetProperties 方法来确定属性的顺序。对于 WPF 设计器,属性是使用 PropertyOrder 类进行排序的。

标准顺序标记由 PropertyOrder 类提供。这些由系统定义的顺序标记包括 EarlyDefaultLate 属性。Early 顺序标记指向“属性”窗口中的较高位置。

没有特定属性顺序的属性将被赋予 Default 顺序。您可以从该类派生并创建自己的自定义顺序标记,从而可以保证属性顺序和属性分组。

示例

下面的代码示例演示如何从 PropertyOrder 派生来实现

用于 WidthHeight 属性的 LayoutSizePriority 类。它是在 Early 顺序之后创建的。因此,它在列表中出现的顺序晚于 Early 属性。LayoutAlignmentPriority 用于 HorizontalAlignmentVerticalAlignment 属性,它是在 LayoutSizePriority 之后创建的。

PropertyOrder 实例通过使用 PropertyOrderAttribute 绑定到属性。CreateBeforeCreateAfter 方法将 Width 放置在 Height 之前,将 HorizontalAlignment 放置在 VerticalAlignment 之前。

Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports Microsoft.Windows.Design.PropertyEditing
Imports Microsoft.Windows.Design.Metadata

Public Class PropertyOrderTokens
    Private Shared layoutSizePriorityValue As PropertyOrder
    Private Shared layoutAlignmentPriorityValue As PropertyOrder


    Public Shared ReadOnly Property LayoutSizePriority() As PropertyOrder
        Get
            If layoutSizePriorityValue Is Nothing Then
                LayoutSizePriority = PropertyOrder.CreateAfter(PropertyOrder.Early)
            End If

            Return layoutSizePriorityValue
        End Get
    End Property


    Public Shared ReadOnly Property LayoutAlignmentPriority() As PropertyOrder
        Get
            If layoutAlignmentPriorityValue Is Nothing Then
                layoutAlignmentPriorityValue = PropertyOrder.CreateAfter(PropertyOrderTokens.LayoutSizePriority)
            End If

            Return layoutAlignmentPriorityValue
        End Get
    End Property
End Class


Friend Class Metadata
    Implements IRegisterMetadata

    ' Called by the designer to register any design-time metadata.
    Public Sub Register() Implements IRegisterMetadata.Register
        Dim builder As New AttributeTableBuilder()

        builder.AddCustomAttributes( _
            GetType(Button), _
            FrameworkElement.HeightProperty, _
            New PropertyOrderAttribute( _
                PropertyOrder.CreateAfter( _
                    PropertyOrderTokens.LayoutSizePriority)))

        builder.AddCustomAttributes( _
            GetType(Button), _
            FrameworkElement.WidthProperty, _
            New PropertyOrderAttribute( _
                PropertyOrder.CreateBefore( _
                    PropertyOrderTokens.LayoutSizePriority)))

        builder.AddCustomAttributes( _
            GetType(Button), _
            FrameworkElement.VerticalAlignmentProperty, _
            New PropertyOrderAttribute( _
                PropertyOrder.CreateAfter( _
                    PropertyOrderTokens.LayoutAlignmentPriority)))

        builder.AddCustomAttributes( _
            GetType(Button), _
            FrameworkElement.HorizontalAlignmentProperty, _
            New PropertyOrderAttribute( _
                PropertyOrder.CreateBefore( _
                    PropertyOrderTokens.LayoutAlignmentPriority)))

        MetadataStore.AddAttributeTable(builder.CreateTable())

    End Sub
End Class
using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Windows.Design.PropertyEditing;
using Microsoft.Windows.Design.Metadata;

public static class PropertyOrderTokens
{
    private static PropertyOrder layoutSizePriority;
    private static PropertyOrder layoutAlignmentPriority;

    public static PropertyOrder LayoutSizePriority
    {
        get
        {
            if (layoutSizePriority == null)
            {
                layoutSizePriority = PropertyOrder.CreateAfter(
                    PropertyOrder.Early);
            }

            return layoutSizePriority;
        }
    }

    public static PropertyOrder LayoutAlignmentPriority
    {
        get
        {
            if (layoutAlignmentPriority == null)
            {
                layoutAlignmentPriority = PropertyOrder.CreateAfter(
                    PropertyOrderTokens.LayoutSizePriority);
            }

            return layoutAlignmentPriority;
        }
    }
}

internal class Metadata : IRegisterMetadata
{
    // Called by the designer to register any design-time metadata.
    public void Register()
    {
        AttributeTableBuilder builder = new AttributeTableBuilder();

        builder.AddCustomAttributes(
            typeof( Button ),
            FrameworkElement.HeightProperty, 
            new PropertyOrderAttribute(
                PropertyOrder.CreateAfter(
                PropertyOrderTokens.LayoutSizePriority)));

        builder.AddCustomAttributes(
            typeof(Button),
            FrameworkElement.WidthProperty, 
            new PropertyOrderAttribute(
                PropertyOrder.CreateBefore(
                PropertyOrderTokens.LayoutSizePriority))); 

        builder.AddCustomAttributes(
            typeof(Button),
            FrameworkElement.VerticalAlignmentProperty, 
            new PropertyOrderAttribute(
                PropertyOrder.CreateAfter(
                PropertyOrderTokens.LayoutAlignmentPriority))); 

        builder.AddCustomAttributes(
            typeof(Button),
            FrameworkElement.HorizontalAlignmentProperty, 
            new PropertyOrderAttribute(
                PropertyOrder.CreateBefore(
                PropertyOrderTokens.LayoutAlignmentPriority)));

        MetadataStore.AddAttributeTable(builder.CreateTable());
    }
}

继承层次结构

System.Object
  Microsoft.Windows.Design.OrderToken
    Microsoft.Windows.Design.PropertyEditing.PropertyOrder

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

另请参见

参考

PropertyOrder 成员

Microsoft.Windows.Design.PropertyEditing 命名空间

其他资源

属性编辑体系结构

WPF 设计器扩展性