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: 
22: 
23: 
24: 
25: 
26: 
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 path = "/FSharpWpfCustomControlLibrary1;" +
               "component/Themes/generic.xaml"
    let uri = new Uri(path, System.UriKind.Relative)
    let resourceDict = Application.LoadComponent(uri) :?> 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
namespace System.ComponentModel
Multiple items
type CustomControl1 =
  inherit obj
  new : unit -> CustomControl1
  override OnApplyTemplate : unit -> 'a

--------------------
new : unit -> CustomControl1
val this : CustomControl1
namespace Microsoft.FSharp.Control
val typeof<'T> : Type
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
  ...

--------------------
Uri(uriString: string) : Uri
Uri(uriString: string, uriKind: UriKind) : Uri
Uri(baseUri: Uri, relativeUri: string) : Uri
Uri(baseUri: Uri, relativeUri: Uri) : Uri
type UriKind =
  | RelativeOrAbsolute = 0
  | Absolute = 1
  | Relative = 2
field UriKind.Relative: UriKind = 2
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

More information

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