共用方式為


DesignModeValueProvider 類別

更新:2007 年 11 月

擷取使用者在設計工具中所做的屬性變更,並於設計階段提供新的值。

命名空間:  Microsoft.Windows.Design.Model
組件:  Microsoft.Windows.Design.Extensibility (在 Microsoft.Windows.Design.Extensibility.dll 中)

語法

Public Class DesignModeValueProvider _
    Inherits FeatureProvider

Dim instance As DesignModeValueProvider
public class DesignModeValueProvider : FeatureProvider
public ref class DesignModeValueProvider : public FeatureProvider
public class DesignModeValueProvider extends FeatureProvider

備註

一般而言,當使用者在設計工具中變更物件的屬性值時,會在設計工具中的物件上設定此值。您可以使用 DesignModeValueProvider 類別,在這個處理序中插入自己的邏輯。例如,您可能希望使用者能夠為控制項將可見的屬性設為 false,但是此控制項在設計階段仍應保持可見狀態。

若要完成這個工作,可以建立 DesignModeValueProvider 並附加至您的自訂控制項。DesignModeValueProvider 會擷取使用者所做的屬性變更,您可以在 TranslatePropertyValue 方法中插入自己的邏輯,而 DesignModeValueProvider 會將新的值傳遞至設計工具。

重要事項:

當您使用這項技術時,設計工具中的屬性行為和 [XAML] 檢視中的屬性值並不相符。[XAML] 檢視會顯示使用者在設計階段輸入的值。[XAML] 檢視中的值表示屬性在執行階段時顯示的行為。

範例

下列範例會建立自訂 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

Namespace CustomButton

    Public Class CustomButtonDesignModeValueProvider
        Inherits DesignModeValueProvider


        Public Sub New()

            Properties.Add(Button.ContentProperty)
            Properties.Add(Button.BackgroundProperty)
        End Sub



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

            If identifier.DependencyProperty Is Button.ContentProperty Then

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

            If identifier.DependencyProperty Is Button.BackgroundProperty Then

                Return New SolidColorBrush(SystemColors.ControlColor)
            End If

            Return MyBase.TranslatePropertyValue(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;   //DesignModeValueProvider
namespace CustomButton
{
    class CustomButtonDesignModeValueProvider : DesignModeValueProvider
    {

        public CustomButtonDesignModeValueProvider()
        {
            Properties.Add(Button.ContentProperty);
            Properties.Add(Button.BackgroundProperty);
        }


        public override object TranslatePropertyValue(PropertyIdentifier identifier, object value)
        {
            if (identifier.DependencyProperty == Button.ContentProperty)
            {
                return ((string)value).ToUpper();
            }

            if (identifier.DependencyProperty == Button.BackgroundProperty)
            {
                return new SolidColorBrush(SystemColors.ControlColor);
            }

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

繼承階層架構

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

執行緒安全

這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。

請參閱

參考

DesignModeValueProvider 成員

Microsoft.Windows.Design.Model 命名空間

其他資源

HOW TO:在設計階段變更屬性行為

WPF 設計工具擴充性架構

屬性編輯架構

功能提供者和功能連接器