Created
April 21, 2017 08:57
-
-
Save hilapon/098a62df8af8ec85982c96d7bb401cd1 to your computer and use it in GitHub Desktop.
[WPF] ComboBox と Enum のバインドのサンプル (列挙体とコンバーター・VB版)
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
Option Explicit On | |
Option Strict On | |
Imports System.ComponentModel | |
Imports System.Globalization | |
Imports System.Runtime.CompilerServices | |
Public Enum SampleEnum | |
<Description("なし")> | |
None | |
<Description("林檎")> | |
Apple | |
<Description("蜜柑")> | |
Orange | |
<Description("葡萄")> | |
Grape | |
<Description("鳳梨")> | |
Pineapple | |
<Description("芭蕉")> | |
Banana | |
End Enum | |
Public Class ModeConverter | |
Implements IValueConverter | |
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert | |
Return CType(value, SampleEnum).GetDescription() | |
End Function | |
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack | |
Throw New NotImplementedException() | |
End Function | |
End Class | |
Module ModeConverterModule | |
<Extension()> | |
Public Function GetDescription(enumobj As [Enum]) As String | |
Dim info = enumobj.GetType().GetField(enumobj.ToString()) | |
Dim array = info.GetCustomAttributes(False) | |
If array.Any() Then | |
Dim attribute = array.First(Function(a) TypeOf a Is DescriptionAttribute) | |
Return CType(attribute, DescriptionAttribute).Description | |
Else | |
Return enumobj.ToString() | |
End If | |
End Function | |
End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment