Skip to content

Instantly share code, notes, and snippets.

@dotcypress
Last active April 6, 2016 20:24
Show Gist options
  • Save dotcypress/7446008 to your computer and use it in GitHub Desktop.
Save dotcypress/7446008 to your computer and use it in GitHub Desktop.
public delegate void PropertyChangedCallback<T>(T sender, DependencyPropertyChangedEventArgs e) where T : DependencyObject;
public static class DependencyPropertyHelper
{
public static DependencyProperty Register<TProperty, TOwner>(string name,
TProperty defaultValue = default(TProperty),
PropertyChangedCallback<TOwner> callback = null) where TOwner : DependencyObject
{
return RegisterImpl(false, name, defaultValue, callback);
}
public static DependencyProperty RegisterAttached<TProperty, TOwner>(string name,
TProperty defaultValue = default(TProperty),
PropertyChangedCallback<TOwner> callback = null) where TOwner : DependencyObject
{
return RegisterImpl(true, name, defaultValue, callback);
}
private static DependencyProperty RegisterImpl<TProperty, TOwner>(bool isAttached,
string name,
TProperty defaultValue = default(TProperty),
PropertyChangedCallback<TOwner> callback = null) where TOwner : DependencyObject
{
var callbackWrapper = callback != null
? (sender, e) => callback((TOwner) sender, e)
: (PropertyChangedCallback) null;
return isAttached
? DependencyProperty.RegisterAttached(name,
typeof (TProperty),
typeof (TOwner),
new PropertyMetadata(defaultValue, callbackWrapper))
: DependencyProperty.Register(name,
typeof (TProperty),
typeof (TOwner),
new PropertyMetadata(defaultValue, callbackWrapper));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment