Silverlight enum string converter
This converter converts Boolean values to Visibility enumeration values. If the Boolean value is false the Convert method will return Visibility. If the Boolean value is true the Convert method will return Visibility. This converter converts values from the Visibility enumeration to Boolean values. The Convert method returns true if the passed value is Visibility. Visible and false otherwise. To invert the results of the converter, set the IsInverted property.
This way, Visibility. Visible will be converted to false and Visibility. Collapsed to true. Converts an Enum value to Visibility enumeration values. If the enum value is one of the values specified in the converter parameter, the Convert method will return Visibility.
Visible , otherwise it will return Visibility. Multiple enum values could be specified in the converter parameter, separated using , or ;. This converter converts Null or empty string values to Visibility enumeration values.
Turns out we need sevaral things: 1 FieldInfo. GetCustomAttributes to retrieve the DescriptionAttribute 2 Caching both the enum value and the description value 3 Differentiate between the conversion targets Combobox. ItemsSource and COmbobox. SelectedItem 4 ConvertBack needs to be implemented as well. Collections; using System. Generic; using System.
ComponentModel; using System. Linq; using Adapt. GetType value ;. IsLiteral ;. GetValue null ;. GetCustomAttributes typeof DescriptionAttribute , false [0]. You are commenting using your WordPress. You are commenting using your Google account. You are commenting using your Twitter account. You are commenting using your Facebook account. Notify me of new comments via email. Notify me of new posts via email.
Christian Harlass's Blog neat. To accomplish this, I am going to enlist the help of a custom MarkupExtension. This extension will simply take an enum Type and then create a bindable list of enum values for my control. Take a look:. GetUnderlyingType this. GetValues actualEnumType ;. CreateInstance actualEnumType, enumValues. Now that we have our enum binding working perfectly without the need for that silly ObjectDataProvider, we can improve our enum experience.
It is very common to have a enum to represent the values, while at the same time, give the enum value a description so that it is more readable to your user. Well, luckily for us, this is easy to. We will just need to use a simple TypeConverter that we can attribute our enum with.
Check it out:. GetField value. ToString ; if fi! GetCustomAttributes typeof DescriptionAttribute , false ; return attributes. IsNullOrEmpty attributes[0]. Description : value.
0コメント