Skip to content

Instantly share code, notes, and snippets.

@allykzam
Created January 4, 2016 14:28
Show Gist options
  • Save allykzam/a35528775f7966adcb1e to your computer and use it in GitHub Desktop.
Save allykzam/a35528775f7966adcb1e to your computer and use it in GitHub Desktop.
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