Udostępnij za pośrednictwem


DependencyPropertyChangedCallback Delegate

Definition

Represents the callback that is invoked when a property value changes, for property change notifications that are registered with the RegisterPropertyChangedCallback technique.

public delegate void DependencyPropertyChangedCallback(DependencyObject ^ sender, DependencyProperty ^ dp);
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
[Windows.Foundation.Metadata.Guid(4032150305, 8603, 23308, 128, 93, 188, 174, 218, 225, 84, 88)]
public delegate void DependencyPropertyChangedCallback(DependencyObject sender, DependencyProperty dp);
Public Delegate Sub DependencyPropertyChangedCallback(sender As DependencyObject, dp As DependencyProperty)

Parameters

sender
DependencyObject

The object instance that holds the property to register for property-changed notification.

dp
DependencyProperty

The dependency property identifier of the property to register for property-changed notification.

Attributes

Examples

This example shows how to use a DependencyPropertyChangedCallback delegate to listen for changes to the Tag property on a TextBlock.

<TextBlock x:Name="textBlock1" Text="Hello, world"/>
long tagToken;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    long tagToken = textBlock1.RegisterPropertyChangedCallback(TextBlock.TagProperty, tbTagChangedCallback);
    base.OnNavigatedTo(e);

    textBlock1.Tag = "name";
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    textBlock1.UnregisterPropertyChangedCallback(TextBlock.TagProperty, tagToken);
    base.OnNavigatedFrom(e);
}

private void tbTagChangedCallback(DependencyObject sender, DependencyProperty dp)
{
    if (dp == TextBlock.TagProperty)
    {
       // These lines produce the same result.
       System.Diagnostics.Debug.WriteLine("The tag has been set to " + ((TextBlock)sender).Tag);
       System.Diagnostics.Debug.WriteLine("The tag has been set to " + sender.GetValue(dp));
    }
}

Remarks

The parameter values of the delegate are based on the parameter given to the RegisterPropertyChangedCallback invocation that registered a particular property for property-changed notification, and the instance that invoked it.

Because the callback has the dp parameter that identifies which property value changed, you can use the same callback to handle multiple property-changed cases, and your logic can write cases for each different property.

For performance reasons, you don't get an OldValue / NewValue property pair from a PropertyChangedCallback method like you do from DependencyPropertyChangedEventArgs. The property value is changed before the callback, so once the method has been invoked, you can call DependencyObject.GetValue to retrieve the new value.

Applies to

See also