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 参数提供为适当的类型。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.

示例

下面的示例定义了一个只读依赖项属性,以及一个 @no__t 0 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 参数提供为适当的类型。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.

适用于