BindingErrorListener In WPF

在WPF里遇到绑定错误,一般我们在调试的时候,会在OutPut(输出)窗口里看到这些绑定错误信息。

于是我下面把这些错误信息显示出来。

BindingErrorListener类

public class BindingErrorListener:TraceListener
   {
       private Action<string> _logAction;

       public static void Listen(Action<string> logAction)
       {
           PresentationTraceSources.DataBindingSource.Listeners.Add(
               new BindingErrorListener() { _logAction = logAction });
       }
       public override void Write(string message)
       {
       }

       public override void WriteLine(string message)
       {
           _logAction(message);
       }
    }

然后在ViewModel或app.xaml.cs里面调用就可以了

  BindingErrorListener.Listen(a => MessageBox.Show(a));

这样就以弹框的形式显示了。

posted @ 2011-09-02 23:54  Lee's Blog  阅读(639)  评论(0编辑  收藏  举报