Last active
April 6, 2016 20:24
-
-
Save dotcypress/7446008 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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