/
CategoriesControl.vb
146 lines (131 loc) · 5.97 KB
/
CategoriesControl.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Imports DevExpress.Xpf.PivotGrid
Imports HowToCreateFieldValueTemplate.CategoryPicturesTableAdapters
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Namespace HowToCreateFieldValueTemplate
Public Class CategoriesControl
Inherits Control
Implements IWeakEventListener
#Region "static staff"
'INSTANT VB NOTE: The variable categoriesTableAdapter was renamed since Visual Basic does not allow variables and other class members to have the same name:
Private Shared categoriesTableAdapter_Renamed As CategoriesTableAdapter
Private Shared categoriesPictures As Dictionary(Of String, ImageSource)
Public Shared ReadOnly ImageSourceProperty As DependencyProperty
Shared Sub New()
DefaultStyleKeyProperty.OverrideMetadata(GetType(CategoriesControl), New FrameworkPropertyMetadata(GetType(CategoriesControl)))
Dim ownerType As Type = GetType(CategoriesControl)
ImageSourceProperty = DependencyProperty.Register("ImageSource", GetType(ImageSource), ownerType, New UIPropertyMetadata())
categoriesPictures = New Dictionary(Of String, ImageSource)()
End Sub
Private Shared ReadOnly Property CategoriesTableAdapter() As CategoriesTableAdapter
Get
If categoriesTableAdapter_Renamed Is Nothing Then
categoriesTableAdapter_Renamed = New CategoriesTableAdapter()
End If
Return categoriesTableAdapter_Renamed
End Get
End Property
Private Shared Function GetImage(ByVal categoryName As String) As ImageSource
If String.IsNullOrEmpty(categoryName) Then
Return Nothing
End If
If categoriesPictures.ContainsKey(categoryName) Then
Return categoriesPictures(categoryName)
Else
Dim icon() As Byte = TryCast(CategoriesTableAdapter.GetIconImageByName(categoryName), Byte())
If icon Is Nothing OrElse icon.Length = 0 Then
Return Nothing
End If
Dim img As BitmapDecoder = New PngBitmapDecoder(New MemoryStream(icon), BitmapCreateOptions.None, BitmapCacheOption.OnLoad)
'INSTANT VB NOTE: The variable imageSource was renamed since Visual Basic does not handle local variables named the same as class members well:
Dim imageSource_Renamed As ImageSource = img.Frames(0)
If imageSource_Renamed.Height < 1 Then
Return Nothing
End If
categoriesPictures.Add(categoryName, imageSource_Renamed)
Return imageSource_Renamed
End If
End Function
#End Region
Public Sub New()
MyBase.New()
FrameworkElementDataContextChangedEventManager.AddListener(Me, Me)
AddHandler Unloaded, AddressOf OnUnloaded
End Sub
Public Property ImageSource() As ImageSource
Get
Return DirectCast(GetValue(ImageSourceProperty), ImageSource)
End Get
Set(ByVal value As ImageSource)
SetValue(ImageSourceProperty, value)
End Set
End Property
Private Sub OnUnloaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
ImageSource = Nothing
End Sub
Private Sub OnDataContextChanged()
SetImageSource()
End Sub
Private Sub SetImageSource()
Dim item As FieldValueElementData = TryCast(Me.DataContext, FieldValueElementData)
If item IsNot Nothing AndAlso Not item.IsOthersRow AndAlso Not String.IsNullOrEmpty(item.DisplayText) Then
ImageSource = CategoriesControl.GetImage(TryCast(item.Value, String))
Else
ImageSource = Nothing
End If
End Sub
#Region "IWeakEventListener Members"
Private Function IWeakEventListener_ReceiveWeakEvent(ByVal managerType As Type, ByVal sender As Object, ByVal e As EventArgs) As Boolean Implements IWeakEventListener.ReceiveWeakEvent
Return OnReceiveWeakEvent(managerType, e)
End Function
Protected Overridable Function OnReceiveWeakEvent(ByVal managerType As Type, ByVal e As EventArgs) As Boolean
If managerType Is GetType(FrameworkElementDataContextChangedEventManager) Then
OnDataContextChanged()
Return True
End If
Return False
End Function
#End Region
#Region "FrameworkElementDataContextChangedEventManager"
Public Class FrameworkElementDataContextChangedEventManager
Inherits WeakEventManager
Private Shared ReadOnly Property CurrentManager() As FrameworkElementDataContextChangedEventManager
Get
Dim managerType As Type = GetType(FrameworkElementDataContextChangedEventManager)
'INSTANT VB NOTE: The local variable currentManager was renamed since Visual Basic will not allow local variables with the same name as their enclosing function or property:
Dim currentManager_Renamed As FrameworkElementDataContextChangedEventManager = CType(WeakEventManager.GetCurrentManager(managerType), FrameworkElementDataContextChangedEventManager)
If currentManager_Renamed Is Nothing Then
currentManager_Renamed = New FrameworkElementDataContextChangedEventManager()
WeakEventManager.SetCurrentManager(managerType, currentManager_Renamed)
End If
Return currentManager_Renamed
End Get
End Property
Private Sub New()
End Sub
Public Shared Sub AddListener(ByVal source As FrameworkElement, ByVal listener As IWeakEventListener)
CurrentManager.ProtectedAddListener(source, listener)
End Sub
Public Shared Sub RemoveListener(ByVal source As FrameworkElement, ByVal listener As IWeakEventListener)
CurrentManager.ProtectedRemoveListener(source, listener)
End Sub
Protected Overrides Sub StartListening(ByVal source As Object)
Dim FrameworkElement As FrameworkElement = DirectCast(source, FrameworkElement)
AddHandler FrameworkElement.DataContextChanged, AddressOf OnDataContextChanged
End Sub
Protected Overrides Sub StopListening(ByVal source As Object)
Dim FrameworkElement As FrameworkElement = DirectCast(source, FrameworkElement)
RemoveHandler FrameworkElement.DataContextChanged, AddressOf OnDataContextChanged
End Sub
Private Sub OnDataContextChanged(ByVal sender As Object, ByVal e As DependencyPropertyChangedEventArgs)
MyBase.DeliverEvent(sender, Nothing)
End Sub
End Class
#End Region
End Class
End Namespace