Attached property

De Banane Atomic
Aller à la navigationAller à la recherche
La version imprimable n’est plus prise en charge et peut comporter des erreurs de génération. Veuillez mettre à jour les signets de votre navigateur et utiliser à la place la fonction d’impression par défaut de celui-ci.

Définition

Une attached property est un type de dependency property.
L'AP est définit dans une classe statique pour pouvoir ensuite être attachée à un DependencyObject.
Utilisé principalement pour décorer un élément avec des métadata qui seront utilisé par un autre élément pour le layout. Ex: Grid.Row.

Example

DockPanel.Dock est conçue pour être définie sur les éléments contenus dans un DockPanel, plutôt que sur le DockPanel proprement dit.

Cs.svg
[AttachedPropertyBrowsableForChildren]
public static Dock GetDock(UIElement element)
{
    if (element == null)
    {
        throw new ArgumentNullException("element");
    }
    return (Dock)element.GetValue(DockPanel.DockProperty);
}

public static void SetDock(UIElement element, Dock dock)
{
    if (element == null)
    {
        throw new ArgumentNullException("element");
    }
    element.SetValue(DockPanel.DockProperty, dock);
}

[CommonDependencyProperty]
public static readonly DependencyProperty DockProperty = DependencyProperty.RegisterAttached(
    "Dock", 
    typeof(Dock), 
    typeof(DockPanel), 
    new FrameworkPropertyMetadata(
        Dock.Left, 
        new PropertyChangedCallback(DockPanel.OnDockChanged)), 
    new ValidateValueCallback(DockPanel.IsValidDock));

snippet propa

Csharp.svg
public static int GetMyProperty(DependencyObject obj)
{
    return (int)obj.GetValue(MyPropertyProperty);
}

public static void SetMyProperty(DependencyObject obj, int value)
{
    obj.SetValue(MyPropertyProperty, value);
}

public static readonly DependencyProperty MyPropertyProperty =
    DependencyProperty.RegisterAttached(
        "MyProperty", 
        typeof(int), 
        typeof(ownerclass), 
        new PropertyMetadata(0));

Code

MyAttachedProperty.cs
// les attached properties sont définies dans des classes à part.
public static class MyAttachedProperty
{
    public static string GetText(DependencyObject obj)
    {
        return (string) obj.GetValue(TextProperty);
    }

    public static void SetText(DependencyObject obj, string value)
    {
        obj.SetValue(TextProperty, value);
    }

    public static readonly DependencyProperty TextProperty = 
        DependencyProperty.RegisterAttached(
            "Text", 
            typeof(string), 
            typeof(MyAttachedProperty),
            new PropertyMetadata("", OnTextChanged));  // valeur par défaut et change handler

    // le change handler n'est appelé que si la valeur a changé (différente de la valeur précédente)
    private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // d: DependencyObject sur lequel l'attached propety a été définie
        if (d is TextBlock tb)
        {
            tb.Text = (string)e.NewValue;
        }
    }
}
MyWindow.xaml
<TextBlock local:MyAttachedProperty.Text="text" />