DependencyObject.SetValue 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
設定相依性屬性的區域值。
多載
SetValue(DependencyProperty, Object) |
設定相依性屬性的區域值 (由相依性屬性的識別碼所指定)。 |
SetValue(DependencyPropertyKey, Object) |
設定唯讀相依性屬性的區域數值 (由相依性屬性的 DependencyPropertyKey 識別項所指定)。 |
SetValue(DependencyProperty, Object)
設定相依性屬性的區域值 (由相依性屬性的識別碼所指定)。
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)
參數
要設定之相依性屬性的識別項。
- value
- Object
新的本機值。
例外狀況
嘗試修改唯讀相依性屬性或密封 DependencyObject 上的屬性。
value
不是針對 dp
屬性註冊的正確類型。
備註
如果提供的型別與原本註冊的相依性屬性所宣告的類型不符,則會擲回例外狀況。 參數 value
應一律以適當的類型提供。
例外狀況可能會受到 ValidateValueCallback 所設定相依性屬性之相依性屬性識別碼上的回呼影響。 否則,所提供的值可能會失敗一般類型檢查條件 (例如,當原生類型為 Double) 時傳遞字串。
適用於
SetValue(DependencyPropertyKey, Object)
設定唯讀相依性屬性的區域數值 (由相依性屬性的 DependencyPropertyKey 識別項所指定)。
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)
參數
要設定之屬性的 DependencyPropertyKey 識別項。
- value
- Object
新的本機值。
範例
下列範例會定義唯讀相依性屬性,以及 public static readonly
DependencyProperty 提供屬性取用者的必要唯讀曝光,以及 CLR 包裝函式的 get 存取子。
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
備註
當您為自訂類別所定義的唯讀相依性屬性設定值時,通常會使用此簽章。 一般而言, SetValue 只會從註冊該相依性屬性的型別呼叫,這會實作內部邏輯,以提供相依性屬性的判定值。 如需詳細資訊,請參閱唯讀相依性屬性。
如果提供的型別與原本註冊的相依性屬性所宣告的類型不符,則會擲回例外狀況。 參數 value
應一律以適當的類型提供。 例外狀況可能會受到 ValidateValueCallback 所設定相依性屬性之相依性屬性識別碼上的回呼影響。
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將推出:在 2024 年,我們將隨著內容的意見反應機制逐步淘汰 GitHub 問題,並以新的意見反應系統來取代。 如需詳細資訊,請參閱提交並檢視相關的意見反應