DesignModeValueProvider 类

[本文档仅供预览,在以后的发行版中可能会发生更改。包含的空白主题用作占位符。]

捕获用户在设计器中进行的属性更改,并在设计时提供新值。

继承层次结构

System.Object
  Microsoft.Windows.Design.Features.FeatureProvider
    Microsoft.Windows.Design.Model.DesignModeValueProvider

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

语法

声明
Public Class DesignModeValueProvider _
    Inherits FeatureProvider
public class DesignModeValueProvider : FeatureProvider
public ref class DesignModeValueProvider : public FeatureProvider
type DesignModeValueProvider =  
    class
        inherit FeatureProvider
    end
public class DesignModeValueProvider extends FeatureProvider

DesignModeValueProvider 类型公开以下成员。

构造函数

  名称 说明
公共方法 DesignModeValueProvider 初始化 DesignModeValueProvider 类的新实例。

页首

属性

  名称 说明
公共属性 Properties 获取要捕获的属性集。

页首

方法

  名称 说明
公共方法 Equals 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
受保护的方法 Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
公共方法 GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
公共方法 GetType 获取当前实例的 Type。 (继承自 Object。)
受保护的方法 InvalidateProperty 使指定的属性无效。
受保护的方法 MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
公共方法 ToString 返回表示当前对象的字符串。 (继承自 Object。)
公共方法 TranslatePropertyValue 捕获用户在设计器中进行的属性更改,并在设计时使用自定义逻辑来提供新值。

页首

备注

当用户在设计器中更改对象的属性值时,通常在设计器中的对象上设置该值。 使用 DesignModeValueProvider 类可以将自己的逻辑插入该进程。 例如,虽然您希望用户能够将控件的可见属性设置为 false,但该控件仍应在设计时可见。

若要完成此操作,需要创建 DesignModeValueProvider 并将它附加到自定义控件。 DesignModeValueProvider 捕获用户所做的属性更改,您将自己的逻辑插入 TranslatePropertyValue 方法中,DesignModeValueProvider 将新值传递到设计器。

重要

使用此技术时,设计器中的属性行为与 XAML 视图中的属性值不匹配。XAML 视图显示用户在设计时输入的值。XAML 视图中的值表示该属性将在运行时展现的行为。

当使用 DesignModeValueProvider 类在设计时更改属性值时,将应用以下限制。

  • 您只可以在由 DependencyObject 类派生的类型上设置设计时值提供程序。

  • 您只能设置依赖属性上的设计时值提供程序。

  • 您的 DesignModeValueProvider 实现必须在基类型属性上设置设计时值。 您可以实现您的值提供程序来以特定的派生类型为目标。 例如,要为 Button 类的 Height 属性注册值提供程序,您必须在 FrameworkElement 类上注册它,并且在值提供程序实现中对目标类型进行测试。 有关更多信息,请参见 演练:在设计时更改属性的行为

  • 值提供程序将按照它们注册的顺序被执行。 最近的为属性注册的值提供程序最后执行,但已经执行所有值提供程序。

  • 如果值提供程序的 TranslatePropertyValue 实现返回 nulla null reference (Nothing in Visual Basic),则属性的设计时值设置为 nulla null reference (Nothing in Visual Basic)。

  • 如果值提供程序的 TranslatePropertyValue 实现返回静态值 UnsetValue, WPF Designer 将在该属性上调用 ClearValue 方法。

备注

如果您正在为 Silverlight 控件编写值提供程序,您必须使用 UnsetValue 的 WPF 版本。这是 WPF Designer 框架的一个限制。

  • 值提供程序并不对 WPF 继承属性适用。 例如,为 FlowDirection 注册值提供程序在设计模式下不能按预期工作。

  • 如果该属性值由绑定设置,值提供程序必须返回 Binding 代替计算值。

  • 由于设计器可能需要将属性强制为特定的设计时值以确保特定的设计体验,因此可能不支持某些值提供程序。 例如,字体属性的自定义值提供程序在 WPF Designer 中不会按预期工作。

  • 当您使用 TypeIdentifier 注册值提供程序时,传递到您的 TranslatePropertyValue 实现的类型标识符可能会与设计时元数据中指定的不同。 将等效,但可能不是同一个实例。 如果您的值提供程序执行类型检查,您必须解析属性标识符的类型组件,并对解析的类型执行类型等效性测试。 通常,您只需检查属性的名称,但如果您必须在类型上执行逻辑,需要解决类型标识符。 请使用 ResolveType 方法来获取正确的类型。

  • WPF Designer 框架可能在几个受支持的格式中的任意一个中传递类型标识符。 如果您的值提供程序执行类型比较,您必须将类型标识符解析到实际类型。 请使用 ResolveType 方法来获取正确的类型。

示例

下面的示例创建会附加到自定义按钮控件中的自定义 DesignModeValueProvider。 在 TranslatePropertyValue 方法中,更改 ButtonContent 属性,以使它在设计器中显示为大写。 您还可以更改 ButtonBackground 属性,以使它在设计器中显示为默认系统颜色。 这些更改只影响设计器。 在运行时,ContentBackground 属性显示为用户设置的值。

有关更多信息,请参见 演练:在设计时更改属性的行为



Imports System
Imports System.Windows                  'SystemColors
Imports System.Windows.Media            'SolidColorBrush
Imports System.Windows.Controls         'Button
Imports Microsoft.Windows.Design.Model  'DesignModeValueProvider
Imports Microsoft.Windows.Design.Metadata


Namespace CustomButton

    Public Class CustomButtonDesignModeValueProvider
        Inherits DesignModeValueProvider


        Public Sub New()
            Properties.Add(GetType(Button), "Content")
            Properties.Add(GetType(Button), "Background")
        End Sub



        Public Overrides Function TranslatePropertyValue( _
            ByVal item As ModelItem, _
            ByVal identifier As PropertyIdentifier, _
            ByVal value As Object) As Object

            If identifier.DeclaringType Is GetType(Button) And _
               identifier.Name = "Content" Then

                Return value.ToString().ToUpper()
            End If

            If identifier.DeclaringType Is GetType(Button) And _
               identifier.Name = "Background" Then

                Return New SolidColorBrush(SystemColors.ControlColor)
            End If

            Return MyBase.TranslatePropertyValue(item, identifier, value)
        End Function
    End Class
End Namespace


using System;
using System.Windows;                   //SystemColors
using System.Windows.Media;             //SolidColorBrush
using System.Windows.Controls;          //Button
using Microsoft.Windows.Design.Model;
using Microsoft.Windows.Design.Metadata;   //DesignModeValueProvider
namespace CustomButton
{
    class CustomButtonDesignModeValueProvider : DesignModeValueProvider
    {

        public CustomButtonDesignModeValueProvider()
        {
            Properties.Add( typeof(Button), "Content");
            Properties.Add(typeof(Button), "Background");
        }


        public override object TranslatePropertyValue(ModelItem item, PropertyIdentifier identifier, object value)
        {
            if (identifier.DeclaringType == typeof( Button ) &&
                identifier.Name == "Content" )
            {
                return ((string)value).ToUpper();
            }

            if (identifier.DeclaringType == typeof(Button) &&
                identifier.Name == "Background")
            {
                return new SolidColorBrush(SystemColors.ControlColor);
            }

            return base.TranslatePropertyValue(item, identifier, value);
        }
    }
}

线程安全

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

请参见

参考

Microsoft.Windows.Design.Model 命名空间

其他资源

如何:在设计时更改属性的行为

WPF 设计器扩展性体系结构

属性编辑体系结构

功能提供程序和功能连接器