Created
January 4, 2016 14:28
-
-
Save allykzam/a35528775f7966adcb1e to your computer and use it in GitHub Desktop.
Union with a TypeConverter for use as a literal in WPF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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