Export (0) Print
Expand All

DependencyPropertyChangedEventArgs Structure

Provides data for various property changed events. Typically these events report effective value changes in the value of a read-only dependency property. Another usage is as part of a PropertyChangedCallback implementation.

Namespace:  System.Windows
Assembly:  WindowsBase (in WindowsBase.dll)

'Declaration
Public Structure DependencyPropertyChangedEventArgs
'Usage
Dim instance As DependencyPropertyChangedEventArgs
You cannot use this managed structure in XAML.

The events that use the DependencyPropertyChangedEventArgs class for event data, and the DependencyPropertyChangedEventHandler method implementations for handlers, generally follow the naming pattern Is*Changed, and are generally implemented as common language runtime (CLR) events without RoutedEvent backing (they are not routed events). Some class handling methods that "handle" otherwise unexposed events that report a state change through a property change, such as ButtonBase.OnIsPressedChanged, also use the DependencyPropertyChangedEventArgs class for event data.

The scenario for PropertyChangedCallback is to use the arguments to report old and new values that come from the property system evaluation of the property. A callback that processes old and new values might choose special handling depending on these values, such as choosing to not respond to value changes that are deemed insignificant.

The following example uses the DependencyPropertyChangedEventArgs class in the context of a PropertyChangedCallback for a particular property of a custom class that also defines events. The callback takes the results of old and new values from the property system as communicated by DependencyPropertyChangedEventArgs, and repackages these into a different events arguments class RoutedPropertyChangedEventArgs(Of T). The new arguments are then used as the data for a "ValueChanged" event defined by and raised by the custom class.

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);
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Community Additions

ADD
Show:
© 2014 Microsoft