Created
October 25, 2012 12:53
-
-
Save benwmaddox/3952393 to your computer and use it in GitHub Desktop.
TraceListener - Display XAML binding issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BindingErrorTraceListener : DefaultTraceListener | |
{ | |
private static BindingErrorTraceListener _Listener; | |
public static void SetTrace() | |
{ SetTrace(SourceLevels.Error, TraceOptions.None); } | |
public static void SetTrace(SourceLevels level, TraceOptions options) | |
{ | |
if (_Listener == null) | |
{ | |
_Listener = new BindingErrorTraceListener(); | |
PresentationTraceSources.DataBindingSource.Listeners.Add(_Listener); | |
} | |
_Listener.TraceOutputOptions = options; | |
PresentationTraceSources.DataBindingSource.Switch.Level = level; | |
} | |
public static void CloseTrace() | |
{ | |
if (_Listener == null) | |
{ return; } | |
_Listener.Flush(); | |
_Listener.Close(); | |
PresentationTraceSources.DataBindingSource.Listeners.Remove(_Listener); | |
_Listener = null; | |
} | |
private StringBuilder _Message = new StringBuilder(); | |
private BindingErrorTraceListener() | |
{ } | |
public override void Write(string message) | |
{ | |
base.Write(message); | |
_Message.Append(message); | |
} | |
public override void WriteLine(string message) | |
{ | |
base.WriteLine(message); | |
if (!message.StartsWith("BindingExpression path error")) | |
return; | |
_Message.Append(message); | |
var final = _Message.ToString(); | |
_Message.Length = 0; | |
MessageBox.Show(final, "Binding Error", MessageBoxButton.OK, | |
MessageBoxImage.Error); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
BindingErrorTraceListener.SetTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment