I have defined a Control with:
static member ItemsProperty : DependencyProperty =
DependencyProperty.Register(
"Items",
typeof<MyMenuItemCollection>,
typeof<MyMenu>,
null);
member this.Items
with get () : MyMenuItemCollection = this.GetValue(MyMenu.ItemsProperty) :?> MyMenuItemCollection
and set (value: MyMenuItemCollection) = this.SetValue(MyMenu.ItemsProperty, value);
The problem occurs on access:
for menuItem in this.Items do
let contentElement: FrameworkElement = menuItem.Content
where I get a null reference exception on this.Items;
‘Items’ threw an exception of type
‘System.NullReferenceException’
Immediately after I initialized in the constructor:
do
this.Items <- new CoolMenuItemCollection()
I think the problem is that
static memberin F# doesn’t correspond to public field as you may have expected, but to a property withgetmember. This means, that each time you acccessthis.ItemsProperty, you’re actually creating a new dependency property.You can create a static field like this: