DependencyObject.SetValue 方法

定義

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

多載

SetValue(DependencyProperty, Object)

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

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)

設定相依性屬性的區域值 (由相依性屬性的識別碼所指定)。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

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

value
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. 否則,所提供的值可能會失敗的一般類型檢查條件 (例如,在原生類型為 Double) 時傳遞字串。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)

設定唯讀相依性屬性的區域數值 (由相依性屬性的 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 識別項。The DependencyPropertyKey identifier of the property to set.

value
Object

新的本機值。The new local value.

範例

下列範例會定義唯讀相依性屬性,以及為 public static readonly DependencyProperty 屬性取用者提供必要的唯讀風險,以及包裝函式的 get 存取子 CLRCLRThe 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.

適用於