I found out that F# 2.0 apparently doesn't support public static fields anymore, which makes impossible the standard way of implementing a DependencyProperty
:
public static readonly FooProperty = DependencyProperty.Register(...); // in C#
I don't quite like one suggested work-around for F# which involves declaring the DependencyProperty
as a static mutable val
and then initializing it with a static do
... (or however exactly it goes).
I've tried exposing a DependencyProperty
as a public static property instead of as a public static field, which seems to work just fine in a WPF application (I've tried data binding and style setters on the property, both with success):
type XY() =
inherit Control()
static let fooProperty =
DependencyProperty.Register("Foo", typeof<string>, typeof<XY>)
static member public FooProperty with get () = fooProperty // see update below:
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // not required!
member public this.Foo with get (): string = this.GetValue(fooProperty) :?> string
and set (x: string) = this.SetValue(fooProperty, x)
Are there any notable drawbacks to publishing a dependency property as a property, instead of as a field? Because this code looks much cleaner to me than the suggested work-around.
Update #1: I just found that in the above code, FooProperty
(the public read-only property) isn't even required. So one could drop the line I high-lighted in the above code example and things still work just fine. I'm now even more curious why people go to such lengths using mutable val
etc. when it's apparently that simple? Am I missing something important?
Update #2: In a comment below, Robert Jeppesen provided a hyperlink to an MSDN page which mentions the following:
If you fail to follow this naming pattern [ie.
Foo
→FooProperty
] , designers might not report your property correctly, and certain aspects of property system style application might not behave as expected.
I put Expression Blend 4 to the test and found that it doesn't seem to be affected at all when the public static FooProperty
is completely missing.