views:

65

answers:

2

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()
A: 

Hi, The CLR getter is NOT called by most of the framework. That is for the convienience of developer access is code behind files.

Inside your constructor would be a great place to initialize the collection if you need to.

Do NOT set a default value (null in your dependency property declaration above) to an empty collection. The default value is a shared, single static instance - so every instance of your control would share that same list, not what you are intending.

Jeff Wilcox
Thank you; I am refamiliarizing myself with .NET by writing a silverlight application in 100% F#. Its proving to be a bigger project than expected. DependencyProperty.Register shows two signatures on MSDN - however my intellisense is only showing a single signature, that takes four parameters. Am I missing something? Or could that be F# mixing the sigs?
akaphenom
That shouldn't matter - your dependency property Register statement is already correct.
Jeff Wilcox
That is what I thought originally but I am having issues as the code as stated is throwing the NullReference exception... SO I think somehting must be off
akaphenom
+1  A: 

I think the problem is that static member in F# doesn't correspond to public field as you may have expected, but to a property with get member. This means, that each time you acccess this.ItemsProperty, you're actually creating a new dependency property.

You can create a static field like this:

type Control = 
  // private static field
  static let itemsProperty : DependencyProperty =  
    DependencyProperty.Register
      ("Items", typeof<MyMenuItemCollection>, typeof<MyMenu>, null); 
  // public static property with getter 
  static member ItemsProperty = itemsProperty  

  // You can use both private 'itemsProperty' field or public property here
  member this.Items 
    with get () : MyMenuItemCollection = 
      this.GetValue(itemsProperty) :?> MyMenuItemCollection 
    and set (value: MyMenuItemCollection) = 
      this.SetValue(itemsProperty, value) 
Tomas Petricek
It fixed the NullReference exception. I think I will need to research the static modifer in F# a little more; control still isn't rendering output - but I am moving forward. Thank you
akaphenom