-
Notifications
You must be signed in to change notification settings - Fork 1
/
CategoriesControl.cs
136 lines (122 loc) · 5.56 KB
/
CategoriesControl.cs
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
using DevExpress.Xpf.PivotGrid;
using HowToCreateFieldValueTemplate.CategoryPicturesTableAdapters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace HowToCreateFieldValueTemplate
{
public class CategoriesControl : Control, IWeakEventListener {
#region static staff
static CategoriesTableAdapter categoriesTableAdapter;
static Dictionary<string, ImageSource> categoriesPictures;
public static readonly DependencyProperty ImageSourceProperty;
static CategoriesControl() {
DefaultStyleKeyProperty.OverrideMetadata(typeof(CategoriesControl),
new FrameworkPropertyMetadata(typeof(CategoriesControl)));
Type ownerType = typeof(CategoriesControl);
ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource),
ownerType, new UIPropertyMetadata());
categoriesPictures = new Dictionary<string, ImageSource>();
}
static CategoriesTableAdapter CategoriesTableAdapter {
get {
if(categoriesTableAdapter == null)
categoriesTableAdapter = new CategoriesTableAdapter();
return categoriesTableAdapter;
}
}
static ImageSource GetImage(string categoryName) {
if(string.IsNullOrEmpty(categoryName)) return null;
if(categoriesPictures.ContainsKey(categoryName)) {
return categoriesPictures[categoryName];
} else {
byte[] icon = CategoriesTableAdapter.GetIconImageByName(categoryName) as byte[];
if(icon == null || icon.Length == 0) {
return null;
}
BitmapDecoder img = new PngBitmapDecoder(new MemoryStream(icon),
BitmapCreateOptions.None,
BitmapCacheOption.OnLoad);
ImageSource imageSource = img.Frames[0];
if(imageSource.Height < 1) return null;
categoriesPictures.Add(categoryName, imageSource);
return imageSource;
}
}
#endregion
public CategoriesControl()
: base() {
FrameworkElementDataContextChangedEventManager.AddListener(this, this);
Unloaded += OnUnloaded;
}
public ImageSource ImageSource {
get { return (ImageSource)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
void OnUnloaded(object sender, RoutedEventArgs e) {
ImageSource = null;
}
void OnDataContextChanged() {
SetImageSource();
}
void SetImageSource() {
FieldValueElementData item = this.DataContext as FieldValueElementData;
if(item != null && !item.IsOthersRow && !string.IsNullOrEmpty(item.DisplayText)) {
ImageSource = CategoriesControl.GetImage(item.Value as string);
} else {
ImageSource = null;
}
}
#region IWeakEventListener Members
bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e) {
return OnReceiveWeakEvent(managerType, e);
}
protected virtual bool OnReceiveWeakEvent(Type managerType, EventArgs e) {
if(managerType == typeof(FrameworkElementDataContextChangedEventManager)) {
OnDataContextChanged();
return true;
}
return false;
}
#endregion
#region FrameworkElementDataContextChangedEventManager
public class FrameworkElementDataContextChangedEventManager : WeakEventManager {
static FrameworkElementDataContextChangedEventManager CurrentManager {
get {
Type managerType = typeof(FrameworkElementDataContextChangedEventManager);
FrameworkElementDataContextChangedEventManager currentManager =
(FrameworkElementDataContextChangedEventManager)WeakEventManager
.GetCurrentManager(managerType);
if(currentManager == null) {
currentManager = new FrameworkElementDataContextChangedEventManager();
WeakEventManager.SetCurrentManager(managerType, currentManager);
}
return currentManager;
}
}
FrameworkElementDataContextChangedEventManager() { }
public static void AddListener(FrameworkElement source, IWeakEventListener listener) {
CurrentManager.ProtectedAddListener(source, listener);
}
public static void RemoveListener(FrameworkElement source, IWeakEventListener listener) {
CurrentManager.ProtectedRemoveListener(source, listener);
}
protected override void StartListening(object source) {
FrameworkElement FrameworkElement = (FrameworkElement)source;
FrameworkElement.DataContextChanged += OnDataContextChanged;
}
protected override void StopListening(object source) {
FrameworkElement FrameworkElement = (FrameworkElement)source;
FrameworkElement.DataContextChanged -= OnDataContextChanged;
}
void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e) {
base.DeliverEvent(sender, null);
}
}
#endregion
}
}