Guest User

Untitled

a guest
Jul 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. Public Class ConversionEventArgs
  2. Inherits EventArgs
  3.  
  4. ''' <summary>
  5. ''' Se restituito come valore, non viene effettuata alcuna operazione sulla source
  6. ''' o sul target del binding
  7. ''' </summary>
  8. Public Shared ReadOnly DoNothing = Binding.DoNothing
  9.  
  10. Public Readonly Property Value As Object
  11. Public Readonly Property TargetType As Type
  12. Public ReadOnly Property Parameter As Object
  13. Public ReadOnly Property Culture As Globalization.CultureInfo
  14. Public Property ConvertedValue As Object
  15.  
  16. Public Sub New(d_value As Object, d_targetType As Type, d_parameter As Object, d_culture As Globalization.CultureInfo)
  17. Value = d_value
  18. TargetType = d_targetType
  19. Parameter = d_parameter
  20. Culture = d_culture
  21. End Sub
  22. End Class
  23.  
  24. ''' <summary>
  25. ''' Converter il cui comportamento è delegato ai metodi che vengono
  26. ''' collegati agli eventi Convert e ConvertBack.
  27. ''' </summary>
  28. ''' <remarks>
  29. ''' Pensato per essere usato dove la conversione richiesta è molto specifica,
  30. ''' es: da variabile di stato a etichetta. Dove applicabile è più manutenibile
  31. ''' usare i converter generici.
  32. ''' </remarks>
  33. Public Class DelegateConverter
  34. Implements IValueConverter
  35.  
  36. ''' <summary>
  37. ''' Usato come valore di default nella proprietà ConvertedValue.
  38. ''' Se ancora presente alla fine della conversione, provoca l'esecuzione
  39. ''' delle operazioni di ripiego per la conversione
  40. ''' </summary>
  41. Private Shared ReadOnly NotSet As New Object
  42.  
  43. ''' <summary>
  44. ''' Chiamato internamente da Convert per effettuare la conversione verso il target
  45. ''' </summary>
  46. Public Event Converting As EventHandler(Of ConversionEventArgs)
  47. ''' <summary>
  48. ''' Chiamato internamente da ConvertBack per effettuare la conversione verso la source
  49. ''' </summary>
  50. Public Event ConvertingBack As EventHandler(Of ConversionEventArgs)
  51.  
  52. Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
  53. Dim _args As New ConversionEventArgs(value, targetType, parameter, culture) With { .ConvertedValue = NotSet }
  54.  
  55. Try
  56. RaiseEvent Converting(Me, _args)
  57. Catch ex As Exception
  58. ' Loggare?
  59. End Try
  60.  
  61. If _args.ConvertedValue Is NotSet
  62. Return DefaultConversion(value, targetType, parameter, culture)
  63. Else
  64. Return _args.ConvertedValue
  65. End If
  66. End Function
  67.  
  68. Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
  69. Dim _args As New ConversionEventArgs(value, targetType, parameter, culture) With { .ConvertedValue = NotSet }
  70.  
  71. Try
  72. RaiseEvent ConvertingBack(Me, _args)
  73. Catch ex As Exception
  74. ' Loggare?
  75. End Try
  76.  
  77. If _args.ConvertedValue Is NotSet
  78. Return DefaultConversion(value, targetType, parameter, culture)
  79. Else
  80. Return _args.ConvertedValue
  81. End If
  82. End Function
  83.  
  84. ''' <summary>
  85. ''' L'operazione di conversione di default, ossia restituire il valore com'è
  86. ''' </summary>
  87. ''' <param name="value"></param>
  88. ''' <param name="targetType"></param>
  89. ''' <param name="parameter"></param>
  90. ''' <param name="culture"></param>
  91. ''' <returns></returns>
  92. Private Function DefaultConversion(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object
  93. Return value
  94. End Function
  95. End Class
Add Comment
Please, Sign In to add comment