Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If we're going to have fancy code generation, I think I would prefer a way to generate DependancyProperties more easily. The snippit I use is certainly helpful, but I'm not a fan how jumbled the code looks when you have on-changed and coerce callbacks, and metadata options. Maybe I'll try and mock up a sample after work.</p> <p><strong>Edit:</strong> Well, here's one concept. It would look a lot more clever if you pass anonymous methods to attributes, but it's still a step up.</p> <p>Before:</p> <pre><code>[DpDefault("The Void")] [DpCoerce(new CoerceValueCallback(MainWindow.CoerceAddress))] [DpChanged(new PropertyChangedCallback(MainWindow.ChangeAddress1))] [DpChanged(new PropertyChangedCallback(MainWindow.ChangeAddress2))] [DpOptions(FrameworkPropertyMetadataOptions.Inherits)] public string Address { get { return Dp.Get&lt;string&gt;(); } set { if (Dp.Get&lt;string&gt;() != value) { Dp.Set(value); PostOffice.SendMailToTheBoss("I moved!"); } } } </code></pre> <p>After:</p> <pre><code>public string Address { get { return (string)GetValue(AddressProperty); } set { if ((string)GetValue(AddressProperty) != value) { SetValue(AddressProperty, value); PostOffice.SendMailToTheBoss("I moved!"); } } } public static readonly DependencyProperty AddressProperty = DependencyProperty.Register("Address", typeof(string), typeof(MainWindow), new FrameworkPropertyMetadata((string)"The Void", FrameworkPropertyMetadataOptions.Inherits, new PropertyChangedCallback(MainWindow.ChangeAddress1) + new PropertyChangedCallback(MainWindow.ChangeAddress2), new CoerceValueCallback(MainWindow.CoerceAddress))); </code></pre> <p>Typically, only the 'DpDefault' attribute would be used, but even if it doesn't make the code shorter, it certainly makes it clearer. Here would be a more typical example:</p> <p>Before:</p> <pre><code>[DpDefault("The Void")] public string Address { get; set; } </code></pre> <p>After:</p> <pre><code>public string Address { get { return (string)GetValue(AddressProperty); } set { SetValue(AddressProperty, value); } } public static readonly DependencyProperty AddressProperty = DependencyProperty.Register("Address", typeof(string), typeof(MainWindow), new UIPropertyMetadata((string)"The Void")); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload