绑定表达式错误的例外

时间:2011-01-03 10:38:26

标签: wpf visual-studio-2010 debugging xaml binding

如果在调试期间发生绑定表达式错误,则会将错误记录到Visual Studio的“输出”窗口中。它看起来像这样:

System.Windows.Data Error: BindingExpression path error: 'User' property not found
on 'MainPageVM' 'MainPageVM' (HashCode=38694667). BindingExpression: Path='User.FullName'
DataItem='MainPageVM' (HashCode=38694667); target element is 'System.Windows.Controls.TextBlock'
Name=''); target property is 'Text' (type 'System.String')..

有没有办法将此错误视为未处理的异常?如果发生绑定错误,我不希望我的Silverlight应用程序继续运行。

1 个答案:

答案 0 :(得分:2)

您可以捕获跟踪错误 (监听器必须在外部DLL中。)

namespace CustomTracer
{
    public class CustomTraceListener : TraceListener
    {
        public CustomTraceListener()
        {
        }

        public override void Write(string message)
        {
        }

        public override void WriteLine(string message)
        {
            if(Debugger.IsAttached)
                Debugger.Break();
        }
    }
}

将此添加到app.config

<system.diagnostics>
    <sources>
      <source name="System.Windows.Data" switchName="OnlyErrors" >
        <listeners>
          <add name="textListener" type="CustomTracer.CustomTraceListener,CustomTracer"/>
        </listeners>
      </source>
    </sources>
    <switches>
      <add name ="OnlyErrors" value ="Error"/>
    </switches>
  </system.diagnostics>