Condividi tramite


DependencyPropertyChangedCallback Delegato

Definizione

Rappresenta il callback richiamato quando viene modificato un valore della proprietà per le notifiche di modifica delle proprietà registrate con la tecnica RegisterPropertyChangedCallback .

public delegate void DependencyPropertyChangedCallback(DependencyObject ^ sender, DependencyProperty ^ dp);
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.Guid(1166556438, 10175, 19393, 172, 38, 148, 193, 96, 31, 58, 73)]
class DependencyPropertyChangedCallback : MulticastDelegate
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Guid(1166556438, 10175, 19393, 172, 38, 148, 193, 96, 31, 58, 73)]
public delegate void DependencyPropertyChangedCallback(DependencyObject sender, DependencyProperty dp);
Public Delegate Sub DependencyPropertyChangedCallback(sender As DependencyObject, dp As DependencyProperty)

Parametri

sender
DependencyObject

Istanza dell'oggetto che contiene la proprietà da registrare per la notifica modificata dalla proprietà.

dp
DependencyProperty

Identificatore della proprietà di dipendenza da registrare per la notifica modificata dalla proprietà.

Attributi

Requisiti Windows

Famiglia di dispositivi
Windows 10 (è stato introdotto in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (è stato introdotto in v1.0)

Esempio

In questo esempio viene illustrato come usare un delegato DependencyPropertyChangedCallback per ascoltare le modifiche apportate alla proprietà Tag in un oggetto 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));
    }
}

Commenti

I valori dei parametri del delegato si basano sul parametro assegnato alla chiamata RegisterPropertyChangedCallback che ha registrato una determinata proprietà per la notifica modificata dalla proprietà e l'istanza che l'ha richiamata.

Poiché il callback ha il parametro dp che identifica il valore della proprietà modificato, è possibile usare lo stesso callback per gestire più case modificate dalle proprietà e la logica può scrivere case per ogni proprietà diversa.

Per motivi di prestazioni, non si ottiene una coppia di proprietà OldValue NewValue / da un metodo PropertyChangedCallback simile a quello eseguito da DependencyPropertyChangedEventArgs. Il valore della proprietà viene modificato prima del callback, quindi una volta richiamato il metodo, è possibile chiamare DependencyObject.GetValue per recuperare il nuovo valore.

Si applica a

Vedi anche