2010-04-30 70 views
7

我在我的文本框有一個ExceptionValidationRuleExceptionValidationRule沒有反應異常

<Window.Resources> 
    <Style x:Key="textStyleTextBox" TargetType="TextBox"> 
     <Style.Triggers> 
      <Trigger Property="Validation.HasError" Value="true"> 
       <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 

<TextBox x:Name="myTextBox" 
    {Binding Path=MyProperty, ValidatesOnExceptions=True}" 
    Style="{StaticResource ResourceKey=textStyleTextBox}" /> 

MyProperty看起來像這樣:

private int myProperty; 

public int MyProperty 
{ 
    get { return myProperty; } 
    set 
    { 
     if(value > 10) 
      throw new ArgumentException("LOL that's an error"); 
     myProperty = value; 
    } 
} 

DEBUG模式,應用程序崩潰與未處理的異常"LOL that's an error"(WPF綁定引擎沒有抓住這一點,我認爲它應該......)。

RELEASE模式下,一切工作正常。

有人可以告訴我,爲什麼地獄這是怎麼回事?我該如何解決這個問題?

+0

您是否附加了UnhandledException事件? – Krimson 2010-12-08 14:09:44

回答

8

該解決方案不是那麼明顯,也沒有很好的記錄,但很簡單。 在調試模式下運行時,Visual Studio打破異常的原因是因爲它是以這種方式配置的。

在調試菜單中,選擇「例外...」。在這個對話框中你可以控制VS如何處理異常。只需取消選中「用戶未處理」「公共語言運行時異常」,按確定並再次運行您的項目。

+0

非常感謝你。由於同樣的問題,我一直在嘲笑我。非常感謝 – Shee 2012-08-24 08:46:08

+0

然後會發生什麼異常被觸發,WPF *不會*優雅地捕捉它。調試器仍然工作嗎? – 2014-03-28 10:16:46