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. 否则,提供的值可能会失败 (例如,将字符串传递时的本机类型为 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) 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.

适用于