90 people like it.
Like the snippet!
WPF / SilverLight Converter II
version 1 is http://fssnip.net/62. This new version support convert from any existing function to a converter function by using composition and pipeline. The convert function is to make the function signature agree to the IValueConverter interface. You can add new functions in the FunctionLibrary module and reuse the class definition to reduce the coding effort.
The first sample is to show how to make the converter pipeline work, the second one is a debugger converter used to debug the data binding problem.
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:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
|
namespace FSharp.Converters
open System
open System.Windows
open System.Windows.Data
open Microsoft.FSharp.Reflection
[<AutoOpen>]
module FunctionLibrary =
let nullFunction = fun value _ _ _ -> value
let stringToInt (a:Object) = Convert.ToInt32(a)
let intToBool = fun i -> i = 0
let boolToVisibility = fun b ->
if b then Visibility.Visible
else Visibility.Collapsed
let convert<'T> f (obj:System.Object) (t:Type) (para:System.Object) (culture:Globalization.CultureInfo) = (obj :?> 'T) |> f |> box
/// 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 StringToVisiblityConverter() =
inherit ConverterBase(stringToInt>>intToBool>>boolToVisibility |> convert, nullFunction)
/// debugger converter used to debug the data binding problem(s)
type DebuggerConverter() =
inherit ConverterBase(nullFunction, nullFunction)
|
namespace Microsoft.FSharp
namespace System
namespace System.Windows
Multiple items
namespace System.Data
--------------------
namespace Microsoft.FSharp.Data
namespace Microsoft
namespace Microsoft.FSharp.Reflection
Multiple items
type AutoOpenAttribute =
inherit Attribute
new : unit -> AutoOpenAttribute
new : path:string -> AutoOpenAttribute
member Path : string
Full name: Microsoft.FSharp.Core.AutoOpenAttribute
--------------------
new : unit -> AutoOpenAttribute
new : path:string -> AutoOpenAttribute
val nullFunction : value:'a -> 'b -> 'c -> 'd -> 'a
Full name: FSharp.Converters.FunctionLibrary.nullFunction
val value : 'a
val stringToInt : a:Object -> int
Full name: FSharp.Converters.FunctionLibrary.stringToInt
val a : Object
Multiple items
type Object =
new : unit -> obj
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member GetType : unit -> Type
member ToString : unit -> string
static member Equals : objA:obj * objB:obj -> bool
static member ReferenceEquals : objA:obj * objB:obj -> bool
Full name: System.Object
--------------------
Object() : unit
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
Convert.ToInt32(value: DateTime) : int
(+0 other overloads)
Convert.ToInt32(value: string) : int
(+0 other overloads)
Convert.ToInt32(value: decimal) : int
(+0 other overloads)
Convert.ToInt32(value: float) : int
(+0 other overloads)
Convert.ToInt32(value: float32) : int
(+0 other overloads)
Convert.ToInt32(value: uint64) : int
(+0 other overloads)
Convert.ToInt32(value: int64) : int
(+0 other overloads)
Convert.ToInt32(value: int) : int
(+0 other overloads)
Convert.ToInt32(value: uint32) : int
(+0 other overloads)
Convert.ToInt32(value: uint16) : int
(+0 other overloads)
val intToBool : i:int -> bool
Full name: FSharp.Converters.FunctionLibrary.intToBool
val i : int
val boolToVisibility : b:bool -> 'a
Full name: FSharp.Converters.FunctionLibrary.boolToVisibility
val b : bool
val convert : f:('T -> obj) -> obj:Object -> t:Type -> para:Object -> culture:Globalization.CultureInfo -> obj
Full name: FSharp.Converters.FunctionLibrary.convert
val f : ('T -> obj)
Multiple items
val obj : Object
--------------------
type obj = Object
Full name: Microsoft.FSharp.Core.obj
val t : Type
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
val para : Object
val culture : Globalization.CultureInfo
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
val box : value:'T -> obj
Full name: Microsoft.FSharp.Core.Operators.box
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: FSharp.Converters.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: FSharp.Converters.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: FSharp.Converters.ConverterBase.ConvertBack
convert a value back
val targetType : 'b
val parameter : 'c
val culture : 'd
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: FSharp.Converters.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
Multiple items
override ConverterBase.Convert : (obj -> Type -> obj -> Globalization.CultureInfo -> obj)
Full name: FSharp.Converters.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: FSharp.Converters.ConverterBase.ConvertBack
override ConverterBase.ConvertBack : (obj -> Type -> obj -> Globalization.CultureInfo -> obj)
Full name: FSharp.Converters.ConverterBase.ConvertBack
Multiple items
type StringToVisiblityConverter =
inherit ConverterBase
new : unit -> StringToVisiblityConverter
Full name: FSharp.Converters.StringToVisiblityConverter
Sample concrete implementation
--------------------
new : unit -> StringToVisiblityConverter
Multiple items
type DebuggerConverter =
inherit ConverterBase
new : unit -> DebuggerConverter
Full name: FSharp.Converters.DebuggerConverter
debugger converter used to debug the data binding problem(s)
--------------------
new : unit -> DebuggerConverter
More information