6 people like it.

WPF/Silverlight Value Converter

Example of a WPF/Silverlight Value Converter base class and concrete implementation.

 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: 
31: 
32: 
33: 
34: 
35: 
36: 
module ConverterBase

open System;
open System.Windows
open System.Windows.Data

let nullFunction = fun value target param culture -> value

/// abstract class for converter
[<AbstractClass>]
type ConverterBase(convertFunction, convertBackFunction) =    
    /// constructor take nullFunction as inputs
    new() = ConverterBase(nullFunction, nullFunction)

    // implement the IValueConverter
    interface IValueConverter with
        /// convert a value to new value
        override this.Convert(value, targetType, parameter, culture) =
            this.Convert value targetType parameter culture

        /// convert a value back
        override this.ConvertBack(value, targetType, parameter, culture) =
            this.ConvertBack value targetType parameter culture
    
    abstract member Convert : (obj -> Type -> obj -> Globalization.CultureInfo -> obj)
    default this.Convert = convertFunction

    abstract member ConvertBack : (obj -> Type -> obj -> Globalization.CultureInfo -> obj)
    default this.ConvertBack = convertBackFunction


/// Sample concrete implementation
type UpperCaseConverter() =
    inherit ConverterBase()
    let convertFunc = fun (v:obj) _ _ _ -> (string v).ToUpper() :> obj
    override this.Convert = convertFunc 
module ConverterBase
namespace System
namespace System.Windows
Multiple items
namespace System.Data

--------------------
namespace Microsoft.FSharp.Data
val nullFunction : value:'a -> target:'b -> param:'c -> culture:'d -> 'a

Full name: ConverterBase.nullFunction
val value : 'a
val target : 'b
val param : 'c
val culture : 'd
Multiple items
type AbstractClassAttribute =
  inherit Attribute
  new : unit -> AbstractClassAttribute

Full name: Microsoft.FSharp.Core.AbstractClassAttribute

--------------------
new : unit -> AbstractClassAttribute
Multiple items
type ConverterBase =
  interface obj
  new : unit -> ConverterBase
  new : convertFunction:(obj -> Type -> obj -> CultureInfo -> obj) * convertBackFunction:(obj -> Type -> obj -> CultureInfo -> obj) -> ConverterBase
  abstract member Convert : (obj -> Type -> obj -> CultureInfo -> obj)
  abstract member ConvertBack : (obj -> Type -> obj -> CultureInfo -> obj)
  override Convert : value:'f * targetType:'g * parameter:'h * culture:'i -> 'j
  override ConvertBack : value:'a * targetType:'b * parameter:'c * culture:'d -> 'e
  override Convert : (obj -> Type -> obj -> CultureInfo -> obj)
  override ConvertBack : (obj -> Type -> obj -> CultureInfo -> obj)

Full name: ConverterBase.ConverterBase


 abstract class for converter


--------------------
new : unit -> ConverterBase


 constructor take nullFunction as inputs

new : convertFunction:(obj -> Type -> obj -> Globalization.CultureInfo -> obj) * convertBackFunction:(obj -> Type -> obj -> Globalization.CultureInfo -> obj) -> ConverterBase
val convertFunction : (obj -> Type -> obj -> Globalization.CultureInfo -> obj)
val convertBackFunction : (obj -> Type -> obj -> Globalization.CultureInfo -> obj)
val this : ConverterBase
Multiple items
override ConverterBase.Convert : value:'f * targetType:'g * parameter:'h * culture:'i -> 'j

Full name: ConverterBase.ConverterBase.Convert


 convert a value to new value


--------------------
type Convert =
  static val DBNull : obj
  static member ChangeType : value:obj * typeCode:TypeCode -> obj + 3 overloads
  static member FromBase64CharArray : inArray:char[] * offset:int * length:int -> byte[]
  static member FromBase64String : s:string -> byte[]
  static member GetTypeCode : value:obj -> TypeCode
  static member IsDBNull : value:obj -> bool
  static member ToBase64CharArray : inArray:byte[] * offsetIn:int * length:int * outArray:char[] * offsetOut:int -> int + 1 overload
  static member ToBase64String : inArray:byte[] -> string + 3 overloads
  static member ToBoolean : value:obj -> bool + 17 overloads
  static member ToByte : value:obj -> byte + 18 overloads
  ...

Full name: System.Convert
val value : 'f
val targetType : 'g
val parameter : 'h
val culture : 'i
Multiple items
property ConverterBase.Convert: obj -> Type -> obj -> Globalization.CultureInfo -> obj

--------------------
override ConverterBase.Convert : value:'f * targetType:'g * parameter:'h * culture:'i -> 'j


 convert a value to new value
override ConverterBase.ConvertBack : value:'a * targetType:'b * parameter:'c * culture:'d -> 'e

Full name: ConverterBase.ConverterBase.ConvertBack


 convert a value back
val targetType : 'b
val parameter : 'c
Multiple items
property ConverterBase.ConvertBack: obj -> Type -> obj -> Globalization.CultureInfo -> obj

--------------------
override ConverterBase.ConvertBack : value:'a * targetType:'b * parameter:'c * culture:'d -> 'e


 convert a value back
Multiple items
abstract member ConverterBase.Convert : (obj -> Type -> obj -> Globalization.CultureInfo -> obj)

Full name: ConverterBase.ConverterBase.Convert

--------------------
type Convert =
  static val DBNull : obj
  static member ChangeType : value:obj * typeCode:TypeCode -> obj + 3 overloads
  static member FromBase64CharArray : inArray:char[] * offset:int * length:int -> byte[]
  static member FromBase64String : s:string -> byte[]
  static member GetTypeCode : value:obj -> TypeCode
  static member IsDBNull : value:obj -> bool
  static member ToBase64CharArray : inArray:byte[] * offsetIn:int * length:int * outArray:char[] * offsetOut:int -> int + 1 overload
  static member ToBase64String : inArray:byte[] -> string + 3 overloads
  static member ToBoolean : value:obj -> bool + 17 overloads
  static member ToByte : value:obj -> byte + 18 overloads
  ...

Full name: System.Convert
type obj = Object

Full name: Microsoft.FSharp.Core.obj
type Type =
  inherit MemberInfo
  member Assembly : Assembly
  member AssemblyQualifiedName : string
  member Attributes : TypeAttributes
  member BaseType : Type
  member ContainsGenericParameters : bool
  member DeclaringMethod : MethodBase
  member DeclaringType : Type
  member Equals : o:obj -> bool + 1 overload
  member FindInterfaces : filter:TypeFilter * filterCriteria:obj -> Type[]
  member FindMembers : memberType:MemberTypes * bindingAttr:BindingFlags * filter:MemberFilter * filterCriteria:obj -> MemberInfo[]
  ...

Full name: System.Type
namespace System.Globalization
Multiple items
type CultureInfo =
  new : name:string -> CultureInfo + 3 overloads
  member Calendar : Calendar
  member ClearCachedData : unit -> unit
  member Clone : unit -> obj
  member CompareInfo : CompareInfo
  member CultureTypes : CultureTypes
  member DateTimeFormat : DateTimeFormatInfo with get, set
  member DisplayName : string
  member EnglishName : string
  member Equals : value:obj -> bool
  ...

Full name: System.Globalization.CultureInfo

--------------------
Globalization.CultureInfo(name: string) : unit
Globalization.CultureInfo(culture: int) : unit
Globalization.CultureInfo(name: string, useUserOverride: bool) : unit
Globalization.CultureInfo(culture: int, useUserOverride: bool) : unit
Multiple items
override ConverterBase.Convert : (obj -> Type -> obj -> Globalization.CultureInfo -> obj)

Full name: ConverterBase.ConverterBase.Convert

--------------------
type Convert =
  static val DBNull : obj
  static member ChangeType : value:obj * typeCode:TypeCode -> obj + 3 overloads
  static member FromBase64CharArray : inArray:char[] * offset:int * length:int -> byte[]
  static member FromBase64String : s:string -> byte[]
  static member GetTypeCode : value:obj -> TypeCode
  static member IsDBNull : value:obj -> bool
  static member ToBase64CharArray : inArray:byte[] * offsetIn:int * length:int * outArray:char[] * offsetOut:int -> int + 1 overload
  static member ToBase64String : inArray:byte[] -> string + 3 overloads
  static member ToBoolean : value:obj -> bool + 17 overloads
  static member ToByte : value:obj -> byte + 18 overloads
  ...

Full name: System.Convert
abstract member ConverterBase.ConvertBack : (obj -> Type -> obj -> Globalization.CultureInfo -> obj)

Full name: ConverterBase.ConverterBase.ConvertBack
override ConverterBase.ConvertBack : (obj -> Type -> obj -> Globalization.CultureInfo -> obj)

Full name: ConverterBase.ConverterBase.ConvertBack
Multiple items
type UpperCaseConverter =
  inherit ConverterBase
  new : unit -> UpperCaseConverter
  override Convert : (obj -> Type -> obj -> CultureInfo -> obj)

Full name: ConverterBase.UpperCaseConverter


 Sample concrete implementation


--------------------
new : unit -> UpperCaseConverter
val convertFunc : (obj -> 'a -> 'b -> 'c -> obj)
val v : obj
Multiple items
val string : value:'T -> string

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

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val this : UpperCaseConverter
Multiple items
override UpperCaseConverter.Convert : (obj -> Type -> obj -> Globalization.CultureInfo -> obj)

Full name: ConverterBase.UpperCaseConverter.Convert

--------------------
type Convert =
  static val DBNull : obj
  static member ChangeType : value:obj * typeCode:TypeCode -> obj + 3 overloads
  static member FromBase64CharArray : inArray:char[] * offset:int * length:int -> byte[]
  static member FromBase64String : s:string -> byte[]
  static member GetTypeCode : value:obj -> TypeCode
  static member IsDBNull : value:obj -> bool
  static member ToBase64CharArray : inArray:byte[] * offsetIn:int * length:int * outArray:char[] * offsetOut:int -> int + 1 overload
  static member ToBase64String : inArray:byte[] -> string + 3 overloads
  static member ToBoolean : value:obj -> bool + 17 overloads
  static member ToByte : value:obj -> byte + 18 overloads
  ...

Full name: System.Convert

More information

Link:http://fssnip.net/62
Posted:13 years ago
Author:Tao Liu and Daniel Mohl
Tags: wpf , silverlight