No more DependencyProperty with AutoDependencyProperty.Fody
Fody plugin that eliminates those verbose dependency property declarations in WPF controls and replaces them with single attribute.
DependencyProperty declarations in WPF are ridiculously redundant and repetitive. Sure there are code snippets that will generate all the boilerplate, but the generated code is a mess to read. The only way to remove the boilerplate while keeping WPF happy is to generate the code during build and that's what my AutoDependencyProperty.Fody is for.
As the name suggests, AutoDependencyProperty.Fody is a Fody plugin. It runs after build and transforms simple automatic C# properties into WPF-friendly dependency properties. Here's what your code looks like with AutoDependencyProperty.Fody:
[AutoDependencyProperty] public bool HelloWorld { get; set; }
And this is what it will look like to WPF when the application is run:
public static readonly DependencyProperty HelloWorldProperty = DependencyProperty.Register( "HelloWorld", typeof(bool), typeof(MyControl)); public bool HelloWorld { get { return (bool)GetValue(HelloWorldProperty); } set { SetValue(HelloWorldProperty, value); } }
Everything should work smoothly. Debugger will step through your code, not the generated code. XAML preview in Visual Studio will recognize the dependency property.
The only issue I have encountered is that TemplateBinding won't compile on generated properties. It's probably syntax-checked before Fody gets the chance to transform the code. You will have to use regular Binding with RelativeSource instead.
You can also apply the property to the class in order to transform all properties in it. You can also specify metadata options like this:
[AutoDependencyProperty(Options = FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)] public bool HelloWorld { get; set; }
In order to enjoy this comfort, get AutoDependencyProperty.Fody from NuGet. It will automatically pull Fody with it, which will setup your project to run AutoDependencyProperty.Fody after build. You can also get sources of AutoDependencyProperty.Fody.