21 people like it.

WPF DepdencyProperty

Declaring WPF DependencyProperty in F#

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
// Consider we have a class "WaterMarkTextBox" and it requires
// a WaterMarkText property.
// Declare the static field for DependencyProperty    
static let WaterMarkTextProperty = DependencyProperty.Register("WaterMarkText", typeof<string>, 
typeof<WaterMarkTextBox>, new PropertyMetadata(String.Empty))

// Gets / Sets the WaterMarkText
member x.WaterMarkText
    with get() = x.GetValue(WaterMarkTextProperty) :?> string
    and set(v) = x.SetValue(WaterMarkTextProperty, v)

// Consider we have a PropertyMetadata for an ItemsSource property 
// with a callback
// Below is the code for ItemsSource Dependency Property
static let itemsSourceMetadata = 
        new PropertyMetadata
            ( null, new PropertyChangedCallback
                ( fun dpo args ->
                    (
                        let box = dpo :?> SearchBox
                        if args.NewValue <> null then
                            box.OnItemsSourceChanged(args.NewValue :?> IEnumerable)
                    )
                )                 
            )
static let ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof<IEnumerable>, 
typeof<SearchBox>, itemsSourceMetadata)

member private x.OnItemsSourceChanged(itemsSource : IEnumerable) =
   (...)
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
module String

from Microsoft.FSharp.Core
val set : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
val box : value:'T -> obj

Full name: Microsoft.FSharp.Core.Operators.box
// Gets / Sets the ItemsSource
member x.ItemsSource
   with get() = x.GetValue(ItemsSourceProperty) :?> IEnumerable
   and set(v) = x.SetValue(ItemsSourceProperty, v)
Raw view Test code New version

More information

Link:http://fssnip.net/3k
Posted:13 years ago
Author:Fahad
Tags: wpf