jongleur1983 / genericdependencyproperties Goto Github PK
View Code? Open in Web Editor NEWexperiments towards using Generics in .NET DependencyProperties
experiments towards using Generics in .NET DependencyProperties
constructors missing
provide generic alternatives to all RegisterAttached() overloads.
OverrideMetadata allows to override the default value and handlers for a DependencyProperty for a given class.
Example code:
IsManipulationEnabledProperty.OverrideMetadata(
typeof(Map),
new FrameworkPropertyMetadata(true));
A Generic replacement could be implemented as an Extension Method like this:
public void OverrideMetadata<TOwner, TProperty>(
this DependencyProperty dp,
GenericFrameworkPropertyMetadata<TProperty, TOwner> metadata)
(see #8)
The Original accepts different TargetTypes, so we might require different implementations here:
Takes a double of returns UnsetValue if it isn't.
If TargetType is double, Convert returns the input value.
If TargetType is object or string the input value is formatted according to some localized string.
This is only slightly connected to the generics approach of this project, but another followup idea.
Usually a Dependency Property is backed by a common .NET property that uses SetValue() and GetValue in it's accessor methods.
It might be worth to think about an attribute on an auto-property instead, like
[DependencyProperty("propertyName")]
public int BackingProperty { get; set; }
instead
PropertyMetada<T> { ... public PropertyMetadata { get { return new PropertyMetadata(...) // but cached } }
An attached DependencyProperty is registered like this:
public static readonly DependencyProperty SomeProperty = DependencyProperty.RegisterAttached(
"Some",
typeof(PropertyType),
typeof(OwnerType)
);
This property can then be used as in
<NodeOfAnyType OwnerType.Some="valueTypeConvertedToPropertyType" />
As there is no property named "Some" on an attached property as far as I understand it yet, the magic string "Some" keeps to be a string literal in code either - or it can be at least a reference to the DependencyProperty itself as a lambda, that is, it could be registered with a generic signature like this:
public static readonly DependencyProperty SomeProperty = DependencyProperty.RegisterAttached<TPropertyType, TOwnerType>(
Func<DependencyProperty> depProperty);
Here TPropertyType replaces typeof(PropertyType, TOwnerType replaces typeof(OwnerType) and the Func is used internally to get "SomeProperty" by reflection and to split that to get the desired result.
Note: It will not be possible to CALL the function in the registration method unfortunately as the static field that is returned by the lambda is null and thus calling the lambda and getting the name is not possible - a classical cylcic dependency.
(see #8)
Converts a JournalEntryStack to a length-limited list.
Not useful as a generic replacement as it's not intended to be used anywhere else than in the NavigationWindow syle, thus it's kind of internal to WPF.
(see #8)
The original takes bool or Nullable and returns a System.Windows.Control.Visibility.
True converts to Visible everything else (null and false) converts to Hidden.
Visible converts to True, everything else converts to False.
For the generic variant I don't know yet how to deal with bool and bool? without losing performance for unnecessary boxing.
Converter for #8
Original: System.Windows.Controls.AlternationConverter
The naming of the original *Metadata classes suggests that there is some kind of relationship on when to use which Metadata class.
This is reflected in http://www.dotnetfunda.com/articles/show/961/wpf-tutorial-dependency-property as well.
We should restrict the generics accordingly to keep the API as clean and type safe as possible.
http://stackoverflow.com/questions/505397/built-in-wpf-ivalueconverters has this list that could be a starting point (to be converted into single issues in future):
AddOwner attaches an existing DependencyProperty to another (additional) owner type.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.