Define an attached DependencyProperty (Silverlight)


Snippet Details

Shortcut
propas
Description
Code snippet for an attached property using DependencyProperty as the backing store
Language
csharp
Types
Expansion
Author
Fons Sonnemans
Upload on
11-12-2013 14:30:37
Downloads
2564

Declarations

ID ToolTip Default
type Property Type object
property Property Name MyProperty
ownerclass The owning class of this Property. Typically the class that it is declared in. ClassNamePlaceholder
defaultvalue The default value for this property. null
targetclass The target class for this property. FrameworkElement

Code Output

        #region $property$ Attached Property

/// <summary> 
/// Identifies the $property$ attachted property. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty $property$Property =
    DependencyProperty.RegisterAttached("$property$", 
                                        typeof($type$), 
                                        typeof($ownerclass$), 
                                        new PropertyMetadata($defaultvalue$, On$property$Changed));

/// <summary>
/// $property$ changed handler. 
/// </summary>
/// <param name="d">$targetclass$ that changed its $property$ attached property.</param>
/// <param name="e">DependencyPropertyChangedEventArgs with the new and old value.</param> 
private static void On$property$Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var source = d as $targetclass$; 
    if (source != null) { 
       var value = ($type$)e.NewValue;
       //TODO: Handle new value. 
    }
}

/// <summary>
/// Gets the value of the $property$ attached property from the specified $targetclass$.
/// </summary>
public static $type$ Get$property$(DependencyObject obj)
{
    return ($type$)obj.GetValue($property$Property);
}


/// <summary>
/// Sets the value of the $property$ attached property to the specified $targetclass$.
/// </summary>
/// <param name="obj">The object on which to set the $property$ attached property.</param>
/// <param name="value">The property value to set.</param>
public static void Set$property$(DependencyObject obj, $type$ value)
{
    obj.SetValue($property$Property, value);
}
 
#endregion $property$ Attached Property

$end$
    

Download Add to .VSIX Package


comments powered by Disqus

Extension Package

No snippets are added


Languages