4 people like it.

WPF Custom Control

This snippet shows how to create a WPF custom control library in F#.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
open System
open System.Collections
open System.Collections.Generic
open System.Collections.ObjectModel
open System.Windows
open System.Windows.Data
open System.Windows.Controls
open System.Windows.Documents
open System.ComponentModel

(* Consider that you have a generic.xaml inside "Themes" folder. For more details on this check this post - http://fadsworld.wordpress.com/2011/03/05/f-wpf-component-development/
*)
type CustomControl1() as this =
    inherit Control()

    do this.DefaultStyleKey <- typeof<CustomControl1>
    let resourceDict = Application.LoadComponent(new Uri("/FSharpWpfCustomControlLibrary1;component/Themes/generic.xaml", System.UriKind.Relative)) :?> ResourceDictionary          
    do this.Resources.MergedDictionaries.Add(resourceDict)

    override this.OnApplyTemplate() =
        printfn "template applied"
namespace System
namespace System.Collections
namespace System.Collections.Generic
namespace System.Collections.ObjectModel
namespace System.Windows
Multiple items
namespace System.Data

--------------------
namespace Microsoft.FSharp.Data
namespace System.ComponentModel
Multiple items
type CustomControl1 =
  inherit obj
  new : unit -> CustomControl1
  override OnApplyTemplate : unit -> 'a

Full name: Script.CustomControl1

--------------------
new : unit -> CustomControl1
val this : CustomControl1
namespace Microsoft.FSharp.Control
val typeof<'T> : Type

Full name: Microsoft.FSharp.Core.Operators.typeof
Multiple items
type Uri =
  new : uriString:string -> Uri + 5 overloads
  member AbsolutePath : string
  member AbsoluteUri : string
  member Authority : string
  member DnsSafeHost : string
  member Equals : comparand:obj -> bool
  member Fragment : string
  member GetComponents : components:UriComponents * format:UriFormat -> string
  member GetHashCode : unit -> int
  member GetLeftPart : part:UriPartial -> string
  ...

Full name: System.Uri

--------------------
Uri(uriString: string) : unit
Uri(uriString: string, uriKind: UriKind) : unit
Uri(baseUri: Uri, relativeUri: string) : unit
Uri(baseUri: Uri, relativeUri: Uri) : unit
type UriKind =
  | RelativeOrAbsolute = 0
  | Absolute = 1
  | Relative = 2

Full name: System.UriKind
field UriKind.Relative = 2
namespace System.Resources
override CustomControl1.OnApplyTemplate : unit -> 'a

Full name: Script.CustomControl1.OnApplyTemplate
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/3i
Posted:8 months ago
Author:Fahad
Tags: f# , wpf