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. @No__t-0 參數應該一律以適當的類型提供。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,提供屬性取用者所需的唯讀公開,以及 @no__t 2 包裝函式的 get 存取子。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. @No__t-0 參數應該一律以適當的類型提供。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.

適用於