DependencyObject.SetValue DependencyObject.SetValue DependencyObject.SetValue DependencyObject.SetValue Method

定義

設定相依性屬性的區域值。Sets the local value of a dependency property.

多載

SetValue(DependencyProperty, Object) SetValue(DependencyProperty, Object) SetValue(DependencyProperty, Object) SetValue(DependencyProperty, Object)

設定相依性屬性的區域值 (由相依性屬性的識別碼所指定)。Sets the local value of a dependency property, specified by its dependency property identifier.

SetValue(DependencyPropertyKey, Object) SetValue(DependencyPropertyKey, Object) SetValue(DependencyPropertyKey, Object) SetValue(DependencyPropertyKey, Object)

設定唯讀相依性屬性的區域數值 (由相依性屬性的 DependencyPropertyKey 識別項所指定)。Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property.

SetValue(DependencyProperty, Object) SetValue(DependencyProperty, Object) SetValue(DependencyProperty, Object) SetValue(DependencyProperty, Object)

設定相依性屬性的區域值 (由相依性屬性的識別碼所指定)。Sets the local value of a dependency property, specified by its dependency property identifier.

public:
 void SetValue(System::Windows::DependencyProperty ^ dp, System::Object ^ value);
public void SetValue (System.Windows.DependencyProperty dp, object value);
member this.SetValue : System.Windows.DependencyProperty * obj -> unit
Public Sub SetValue (dp As DependencyProperty, value As Object)

參數

dp
DependencyProperty DependencyProperty DependencyProperty DependencyProperty

要設定之相依性屬性的識別項。The identifier of the dependency property to set.

value
Object Object Object Object

新區域數值。The new local value.

例外狀況

嘗試修改唯讀的相依性屬性,或密封的 DependencyObject 上的屬性。Attempted to modify a read-only dependency property, or a property on a sealed DependencyObject.

value 不是如同為 dp 屬性所註冊的正確型別。value was not the correct type as registered for the dp property.

備註

如果提供的型別不符合宣告相依性屬性為最初註冊的型別,則會擲回例外狀況。If the provided type does not match the type that is declared for the dependency property as it was originally registered, an exception is thrown. value參數應該一律提供適當的型別。The value parameter should always be provided as the appropriate type.

例外狀況可能會受到ValidateValueCallback存在於所設定之相依性屬性的相依性屬性識別碼的回呼。The exception conditions are potentially influenced by the ValidateValueCallback callback that exists on the dependency property identifier of the dependency property being set. 否則,所提供的值可能會失敗 (例如,將字串傳遞時的原生型別是雙精度浮點數) 的一般型別檢查條件。Otherwise, the value provided might be failing general type-checking conditions (for example, passing a string when the native type is Double).

SetValue(DependencyPropertyKey, Object) SetValue(DependencyPropertyKey, Object) SetValue(DependencyPropertyKey, Object) SetValue(DependencyPropertyKey, Object)

設定唯讀相依性屬性的區域數值 (由相依性屬性的 DependencyPropertyKey 識別項所指定)。Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property.

public:
 void SetValue(System::Windows::DependencyPropertyKey ^ key, System::Object ^ value);
public void SetValue (System.Windows.DependencyPropertyKey key, object value);
member this.SetValue : System.Windows.DependencyPropertyKey * obj -> unit
Public Sub SetValue (key As DependencyPropertyKey, value As Object)

參數

key
DependencyPropertyKey DependencyPropertyKey DependencyPropertyKey DependencyPropertyKey

要設定之屬性的 DependencyPropertyKey 識別項。The DependencyPropertyKey identifier of the property to set.

value
Object Object Object Object

新區域數值。The new local value.

範例

下列範例會定義唯讀的相依性屬性,連同public static readonly DependencyProperty ,提供必要的唯讀曝光的 get 存取子屬性取用者,來CLRCLR包裝函式。The following example defines a read-only dependency property, along with a public static readonly DependencyProperty that provides necessary read-only exposure to property consumers, and the get accessor for the CLRCLR wrapper.

internal static readonly DependencyPropertyKey AquariumSizeKey = DependencyProperty.RegisterReadOnly(
  "AquariumSize",
  typeof(double),
  typeof(Aquarium),
  new PropertyMetadata(double.NaN)
);
public static readonly DependencyProperty AquariumSizeProperty =
  AquariumSizeKey.DependencyProperty;
public double AquariumSize
{
  get { return (double)GetValue(AquariumSizeProperty); }
}
Friend Shared ReadOnly AquariumSizeKey As DependencyPropertyKey = DependencyProperty.RegisterReadOnly("AquariumSize", GetType(Double), GetType(Aquarium), New PropertyMetadata(Double.NaN))
Public Shared ReadOnly AquariumSizeProperty As DependencyProperty = AquariumSizeKey.DependencyProperty
Public ReadOnly Property AquariumSize() As Double
    Get
        Return CDbl(GetValue(AquariumSizeProperty))
    End Get
End Property

備註

當您設定您的自訂類別所定義的唯讀相依性屬性的值,通常使用此簽章。This signature is generally used when you set values for read-only dependency properties that are defined by your custom classes. 一般而言,SetValue只會從已註冊該相依性屬性,它會實作內部邏輯可決定的值相依性屬性的型別呼叫。Generally, SetValue is called only from the type that registered that dependency property, which implements the internal logic that provides the determined value for the dependency property. 如需詳細資訊,請參閱唯讀相依性屬性For more information, see Read-Only Dependency Properties.

如果提供的型別不符合宣告相依性屬性為最初註冊的型別,則會擲回例外狀況。If the provided type does not match the type that is declared for the dependency property as it was originally registered, an exception is thrown. value參數應該一律提供適當的型別。The value parameter should always be provided as the appropriate type. 例外狀況可能會受到ValidateValueCallback存在於所設定之相依性屬性的相依性屬性識別碼的回呼。The exception conditions are potentially influenced by the ValidateValueCallback callback that exists on the dependency property identifier of the dependency property being set.

適用於