27
|
1 // http://www.switchonthecode.com/tutorials/wpf-snippet-detecting-binding-errors
|
|
2
|
|
3 using System.Diagnostics;
|
|
4 using System.Text;
|
|
5 using System.Windows;
|
|
6
|
|
7 namespace Common.Xaml
|
|
8 {
|
|
9 public class BindingErrorTraceListener : DefaultTraceListener
|
|
10 {
|
|
11 private static BindingErrorTraceListener _listener;
|
|
12
|
|
13
|
|
14 private readonly StringBuilder _message = new StringBuilder();
|
|
15
|
|
16 private BindingErrorTraceListener()
|
|
17 {
|
|
18 }
|
|
19
|
|
20 public override void Write(string message)
|
|
21 {
|
|
22 _message.Append(message);
|
|
23 }
|
|
24
|
|
25 public override void WriteLine(string message)
|
|
26 {
|
|
27 _message.Append(message);
|
|
28
|
|
29 var final = _message.ToString();
|
|
30 _message.Length = 0;
|
|
31
|
|
32 MessageBox.Show(final,
|
|
33 "Binding Error",
|
|
34 MessageBoxButton.OK,
|
|
35 MessageBoxImage.Error);
|
|
36 }
|
|
37
|
|
38 public static void CloseTrace()
|
|
39 {
|
|
40 if (_listener == null)
|
|
41 {
|
|
42 return;
|
|
43 }
|
|
44
|
|
45 _listener.Flush();
|
|
46 _listener.Close();
|
|
47 PresentationTraceSources.DataBindingSource.Listeners.Remove(_listener);
|
|
48 _listener = null;
|
|
49 }
|
|
50
|
|
51 [Conditional("DEBUG")]
|
|
52 public static void SetTrace()
|
|
53 {
|
|
54 SetTrace(SourceLevels.Error, TraceOptions.None);
|
|
55 }
|
|
56
|
|
57 public static void SetTrace(SourceLevels level, TraceOptions options)
|
|
58 {
|
|
59 if (_listener == null)
|
|
60 {
|
|
61 _listener = new BindingErrorTraceListener();
|
|
62 PresentationTraceSources.DataBindingSource.Listeners.Add(_listener);
|
|
63 }
|
|
64
|
|
65 _listener.TraceOutputOptions = options;
|
|
66 PresentationTraceSources.DataBindingSource.Switch.Level = level;
|
|
67 }
|
|
68 }
|
|
69 }
|