共用方式為


DependencyPropertyChangedEventArgs 結構

定義

提供各種屬性變更事件的資料。 這些事件通常會報告唯讀相依性屬性值中的有效值變更。 此外,也可以當做 PropertyChangedCallback 實作的一部分來使用。

public value class DependencyPropertyChangedEventArgs
public struct DependencyPropertyChangedEventArgs
type DependencyPropertyChangedEventArgs = struct
Public Structure DependencyPropertyChangedEventArgs
繼承
DependencyPropertyChangedEventArgs

範例

下列範例會 DependencyPropertyChangedEventArgs 針對也會定義事件之自訂類別的特定屬性,在 的內容 PropertyChangedCallback 中使用 類別。 回呼會根據 的 DependencyPropertyChangedEventArgs 通訊,從 屬性系統取得舊值和新值的結果,並將這些值重新封裝成不同的事件引數類別 RoutedPropertyChangedEventArgs<T> 。 然後,新的引數會當做由 自訂類別所定義的 「ValueChanged」 事件資料使用。

public static readonly DependencyProperty ValueProperty =
    DependencyProperty.Register(
        "Value", typeof(decimal), typeof(NumericUpDown),
        new FrameworkPropertyMetadata(MinValue, new PropertyChangedCallback(OnValueChanged),
                                      new CoerceValueCallback(CoerceValue)));

private static object CoerceValue(DependencyObject element, object value)
{
    decimal newValue = (decimal)value;

    newValue = Math.Max(MinValue, Math.Min(MaxValue, newValue));

    return newValue;
}

private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
    NumericUpDown control = (NumericUpDown)obj;

    RoutedPropertyChangedEventArgs<decimal> e = new RoutedPropertyChangedEventArgs<decimal>(
        (decimal)args.OldValue, (decimal)args.NewValue, ValueChangedEvent);
    control.OnValueChanged(e);
}
/// <summary>
/// Identifies the ValueChanged routed event.
/// </summary>
public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent(
    "ValueChanged", RoutingStrategy.Bubble,
    typeof(RoutedPropertyChangedEventHandler<decimal>), typeof(NumericUpDown));

/// <summary>
/// Occurs when the Value property changes.
/// </summary>
public event RoutedPropertyChangedEventHandler<decimal> ValueChanged
{
    add { AddHandler(ValueChangedEvent, value); }
    remove { RemoveHandler(ValueChangedEvent, value); }
}
/// <summary>
/// Raises the ValueChanged event.
/// </summary>
/// <param name="args">Arguments associated with the ValueChanged event.</param>
protected virtual void OnValueChanged(RoutedPropertyChangedEventArgs<decimal> args)
{
    RaiseEvent(args);
}
Public Shared ReadOnly ValueProperty As DependencyProperty = DependencyProperty.Register("Value", GetType(Decimal), GetType(NumericUpDown), New FrameworkPropertyMetadata(MinValue, New PropertyChangedCallback(AddressOf OnValueChanged), New CoerceValueCallback(AddressOf CoerceValue)))

Private Shared Overloads Function CoerceValue(ByVal element As DependencyObject, ByVal value As Object) As Object
    Dim newValue As Decimal = CDec(value)

    newValue = Math.Max(MinValue, Math.Min(MaxValue, newValue))

    Return newValue
End Function

Private Shared Sub OnValueChanged(ByVal obj As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs)
    Dim control As NumericUpDown = CType(obj, NumericUpDown)

    Dim e As New RoutedPropertyChangedEventArgs(Of Decimal)(CDec(args.OldValue), CDec(args.NewValue), ValueChangedEvent)
    control.OnValueChanged(e)
End Sub
''' <summary>
''' Identifies the ValueChanged routed event.
''' </summary>
Public Shared ReadOnly ValueChangedEvent As RoutedEvent = EventManager.RegisterRoutedEvent("ValueChanged", RoutingStrategy.Bubble, GetType(RoutedPropertyChangedEventHandler(Of Decimal)), GetType(NumericUpDown))

''' <summary>
''' Occurs when the Value property changes.
''' </summary>
Public Custom Event ValueChanged As RoutedPropertyChangedEventHandler(Of Decimal)
    AddHandler(ByVal value As RoutedPropertyChangedEventHandler(Of Decimal))
        MyBase.AddHandler(ValueChangedEvent, value)
    End AddHandler
    RemoveHandler(ByVal value As RoutedPropertyChangedEventHandler(Of Decimal))
        MyBase.RemoveHandler(ValueChangedEvent, value)
    End RemoveHandler
    RaiseEvent(ByVal sender As System.Object, ByVal e As RoutedPropertyChangedEventArgs(Of Decimal))
    End RaiseEvent
End Event
''' <summary>
''' Raises the ValueChanged event.
''' </summary>
''' <param name="args">Arguments associated with the ValueChanged event.</param>
Protected Overridable Sub OnValueChanged(ByVal args As RoutedPropertyChangedEventArgs(Of Decimal))
    MyBase.RaiseEvent(args)
End Sub

備註

針對事件資料使用 DependencyPropertyChangedEventArgs 類別的事件,以及 DependencyPropertyChangedEventHandler 處理常式的方法實作通常會遵循命名模式 Is*Changed ,而且通常會實作為 Common Language Runtime (CLR) 事件,而不 RoutedEvent 需 (它們不是路由事件) 。 某些類別處理方法,這些方法「處理」否則為未公開的事件,這些事件會透過屬性變更報告狀態變更,例如 ButtonBase.OnIsPressedChanged ,也會針對事件資料使用 DependencyPropertyChangedEventArgs 類別。

的案例 PropertyChangedCallback 是使用 引數來報告來自 屬性之屬性系統評估的舊值和新值。 處理舊值和新值的回呼可能會根據這些值選擇特殊處理,例如選擇不回應視為不重要的值變更。

建構函式

DependencyPropertyChangedEventArgs(DependencyProperty, Object, Object)

初始化 DependencyPropertyChangedEventArgs 類別的新執行個體。

屬性

NewValue

取得變更後的屬性值。

OldValue

取得變更前的屬性值。

Property

取得發生值變更之相依性屬性的識別項。

方法

Equals(DependencyPropertyChangedEventArgs)

判斷提供的 DependencyPropertyChangedEventArgs 和目前的 DependencyPropertyChangedEventArgs 是否相等。

Equals(Object)

判斷提供的物件和目前的 DependencyPropertyChangedEventArgs 是否相等。

GetHashCode()

取得這個 DependencyPropertyChangedEventArgs 的雜湊碼。

運算子

Equality(DependencyPropertyChangedEventArgs, DependencyPropertyChangedEventArgs)

判斷兩個指定的 DependencyPropertyChangedEventArgs 物件是否具有相同的值。

Inequality(DependencyPropertyChangedEventArgs, DependencyPropertyChangedEventArgs)

判斷兩個指定的 DependencyPropertyChangedEventArgs 物件是否不相等。

適用於

另請參閱