DependencyObject.RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback) DependencyObject.RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback) DependencyObject.RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback) DependencyObject.RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback) Method

Definition

Registers a notification function for listening to changes to a specific DependencyProperty on this DependencyObject instance.

public : long RegisterPropertyChangedCallback(DependencyProperty dp, DependencyPropertyChangedCallback callback)
long RegisterPropertyChangedCallback(DependencyProperty dp, DependencyPropertyChangedCallback callback) const;
public long RegisterPropertyChangedCallback(DependencyProperty dp, DependencyPropertyChangedCallback callback)
Public Function RegisterPropertyChangedCallback(dp As DependencyProperty, callback As DependencyPropertyChangedCallback) As long
Parameters
dp
DependencyProperty DependencyProperty

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

callback
DependencyPropertyChangedCallback DependencyPropertyChangedCallback

A callback based on the DependencyPropertyChangedCallback delegate, which the system invokes when the value of the specified property changes.

Returns
long long

A token that represents the callback, used to identify the callback in calls to UnregisterPropertyChangedCallback.

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

Use RegisterPropertyChangedCallback to get property-changed notification for dependency properties that are already defined as part of the Windows Runtime. This is useful for properties where were isn't already a corresponding Windows Runtime event that tracks changes. For example, FrameworkElement.Tag is an existing Windows Runtime dependency property, and your app could track when that property's value changes because some external input (like a data binding) has changed that property's runtime value on a particular object instance that's part of your app UI.

To unregister the callback, call UnregisterPropertyChangedCallback and pass in the token returned by this method.

You don't typically use RegisterPropertyChangedCallback for notifications on a custom dependency property, because custom dependency properties already have a way to register a property-changed handler that provides more data in the event args. For more info, see Custom dependency properties.

See Also