DependencyObject.RegisterPropertyChangedCallback Method

Definition

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

public:
 virtual long long RegisterPropertyChangedCallback(DependencyProperty ^ dp, DependencyPropertyChangedCallback ^ callback) = RegisterPropertyChangedCallback;
public long RegisterPropertyChangedCallback(DependencyProperty dp, DependencyPropertyChangedCallback callback);
function registerPropertyChangedCallback(dp, callback)
Public Function RegisterPropertyChangedCallback (dp As DependencyProperty, callback As DependencyPropertyChangedCallback) As Long

Parameters

dp
DependencyProperty

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

callback
DependencyPropertyChangedCallback

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

Returns

Int64

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)
{
    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 XAML framework. This is useful for properties where were isn't already a corresponding XAML framework event that tracks changes. For example, FrameworkElement.Tag is an existing XAML framework 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.

Applies to

See also