Instantly share code, notes, and snippets.

Embed
What would you like to do?
Union with a TypeConverter for use as a literal in WPF
open System.ComponentModel
[<TypeConverter(typedefof<TabsConverter>)>]
type Tabs =
| Item
| Hour
| Event
| Search
and TabsConverter() =
inherit TypeConverter()
override __.CanConvertFrom (context, sourceType) =
if sourceType = typedefof<string> then true
else base.CanConvertFrom (context, sourceType)
override __.ConvertFrom (context, culture, value) =
match value with
| :? string as x ->
match x with
| "Item" -> Item :> obj
| "Hour" -> Hour :> obj
| "Event" -> Event :> obj
| "Search" -> Search :> obj
| _ -> base.ConvertFrom (context, culture, value)
| _ -> base.ConvertFrom (context, culture, value)
override __.ConvertTo (context, culture, value, destinationType) =
if destinationType <> typedefof<string> then base.ConvertTo (context, culture, value, destinationType)
else
match value with
| :? Tabs as x -> (sprintf "%A" x) :> obj
| _ -> base.ConvertTo (context, culture, value, destinationType)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment