comparison Workflow/Client/Controls/UIMessage.xaml.cs @ 89:3c67e54e3a17

Working version
author stevenhollidge <stevenhollidge@hotmail.com>
date Mon, 30 Apr 2012 10:45:32 +0100
parents
children
comparison
equal deleted inserted replaced
88:e84dc4926a5a 89:3c67e54e3a17
1 using System;
2 using System.Windows;
3 using System.Windows.Controls;
4 using System.Windows.Media;
5 using System.Windows.Media.Animation;
6 using System.Windows.Media.Imaging;
7
8 namespace Client.Controls
9 {
10 public enum MessageType
11 {
12 NotSet = 0,
13 Error,
14 Info,
15 Success,
16 Question
17 }
18
19 public partial class UIMessage : UserControl
20 {
21 #region Fields
22
23 private SolidColorBrush _errorBrush;
24 private SolidColorBrush _infoBrush;
25 private SolidColorBrush _successBrush;
26 private SolidColorBrush _questionBrush;
27
28 private BitmapImage _errorImage;
29 private BitmapImage _infoImage;
30 private BitmapImage _successImage;
31 private BitmapImage _questionImage;
32
33 private Storyboard _fadeInStoryboard;
34 private Storyboard _fadeOutStoryboard;
35
36 #endregion
37
38 #region Constructor
39
40 public UIMessage()
41 {
42 InitializeComponent();
43 CheckResourcesAreAvailable();
44
45 _fadeInStoryboard.Completed += (sender, args) => _fadeOutStoryboard.Begin();
46 }
47
48 #endregion
49
50 #region Dependency Properties: MessageType, Text
51
52 public static readonly DependencyProperty TypeProperty =
53 DependencyProperty.Register("MessageType",
54 typeof(MessageType),
55 typeof(UIMessage),
56 new PropertyMetadata(MessageType.NotSet, TypePropertyChanged));
57
58 private static void TypePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
59 {
60 var uiMessage = (UIMessage)dependencyObject;
61 uiMessage.Display.SetValue(Border.BackgroundProperty, uiMessage.GetBrush(uiMessage.MessageType));
62 uiMessage.Icon.SetValue(Image.SourceProperty, uiMessage.GetImage(uiMessage.MessageType));
63 }
64
65 public MessageType MessageType
66 {
67 get { return (MessageType)GetValue(TypeProperty); }
68 set { SetValue(TypeProperty, value); }
69 }
70
71 public static readonly DependencyProperty TextProperty =
72 DependencyProperty.Register("Text",
73 typeof(string),
74 typeof(UIMessage),
75 new PropertyMetadata(default(string)));
76
77 public string Text
78 {
79 get { return (string)GetValue(TextProperty); }
80 set { SetValue(TextProperty, value); }
81 }
82
83 #endregion
84
85 #region Methods
86
87 private void CheckResourcesAreAvailable()
88 {
89 _errorBrush = (SolidColorBrush)this.Resources["ErrorBrush"];
90 _infoBrush = (SolidColorBrush)this.Resources["InfoBrush"];
91 _successBrush = (SolidColorBrush)this.Resources["SuccessBrush"];
92 _questionBrush = (SolidColorBrush)this.Resources["QuestionBrush"];
93 _errorImage = (BitmapImage)this.Resources["ErrorImage"];
94 _infoImage = (BitmapImage)this.Resources["InfoImage"];
95 _successImage = (BitmapImage)this.Resources["SuccessImage"];
96 _questionImage = (BitmapImage)this.Resources["QuestionImage"];
97 _fadeInStoryboard = (Storyboard)this.Resources["FadeIn"];
98 _fadeOutStoryboard = (Storyboard)this.Resources["FadeOut"];
99
100 if (_errorBrush == null) throw new Exception("Missing ErrorBrush resource");
101 if (_infoBrush == null) throw new Exception("Missing InfoBrush resource");
102 if (_successBrush == null) throw new Exception("Missing SuccessBrush resource");
103 if (_questionBrush == null) throw new Exception("Missing QuestionBrush resource");
104 if (_errorImage == null) throw new Exception("Missing BitmapImage ErrorImage resource");
105 if (_infoImage == null) throw new Exception("Missing BitmapImage InfoImage resource");
106 if (_successImage == null) throw new Exception("Missing BitmapImage SuccessImage resource");
107 if (_questionImage == null) throw new Exception("Missing BitmapImage QuestionImage resource");
108 if (_fadeInStoryboard == null) throw new Exception("Missing Storyboard FadeIn resource");
109 if (_fadeOutStoryboard == null) throw new Exception("Missing Storyboard FadeOut resource");
110 }
111
112 private SolidColorBrush GetBrush(MessageType type)
113 {
114 SolidColorBrush brush = null;
115 switch (type)
116 {
117 case MessageType.Info:
118 brush = _infoBrush;
119 break;
120 case MessageType.Error:
121 brush = _errorBrush;
122 break;
123 case MessageType.Success:
124 brush = _successBrush;
125 break;
126 case MessageType.Question:
127 brush = _questionBrush;
128 break;
129 }
130 return brush;
131 }
132
133 private BitmapImage GetImage(MessageType type)
134 {
135 BitmapImage image = null;
136 switch (type)
137 {
138 case MessageType.Info:
139 image = _infoImage;
140 break;
141 case MessageType.Error:
142 image = _errorImage;
143 break;
144 case MessageType.Success:
145 image = _successImage;
146 break;
147 case MessageType.Question:
148 image = _questionImage;
149 break;
150 }
151 return image;
152 }
153
154 public void Show()
155 {
156 _fadeInStoryboard.Begin();
157 }
158
159 #endregion
160 }
161 }