2010-11-26 44 views
1

我已經定義的控制模板表示驗證錯誤:爲什麼我的WPF驗證錯誤沒有顯示?

<TextBox Text="{Binding Text}" PreviewTextInput="textBox1_PreviewTextInput" 
     Validation.ErrorTemplate="{StaticResource validationTemplate}" /> 

我設置從代碼驗證後面如下::

<ControlTemplate x:Key="validationTemplate"> 
        <DockPanel LastChildFill="True"> 
         <TextBlock DockPanel.Dock="Right" 
            Background="Red" 
            TextWrapping="Wrap"> 
          <TextBlock.Text> 
           <Binding Path="(Validation.Errors)[0].ErrorContent" 
     RelativeSource="{x:Static RelativeSource.Self}">           
           </Binding> 
          </TextBlock.Text>         
         </TextBlock> 
         <AdornedElementPlaceholder ></AdornedElementPlaceholder> 
        </DockPanel> 
</ControlTemplate> 

如下我已經定義一個TextBox

private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e) 
    { 
     TextBox txtBox = (TextBox)sender; 
    .... 
    .... 

    ValidationError validationError = new ValidationError(new DummyValidator(), 
      txtBox.GetBindingExpression(TextBox.TextProperty)); 
     Validation.MarkInvalid(txtBox.GetBindingExpression(TextBox.TextProperty), validationError);    
     validationError.ErrorContent = "This is wrong input"; 
     e.Handled = true; 
    } 


現在的問題是驗證正在被解僱,並顯示一條紅色條帶,但裏面的錯誤信息沒有得到顯示! 可能是我錯這個
它拋出一些例外控制檯(索引越界異常)

<Binding Path="(Validation.Errors)[0].ErrorContent" 
     RelativeSource="{x:Static RelativeSource.Self}"> 

請指引我關於我要去哪裏錯了?

回答

0
<Binding Path="(Validation.Errors)[0].ErrorContent" 
    RelativeSource="{x:Static RelativeSource.Self}"> 

你的RelativeSource是錯誤的

<Binding Path="(Validation.Errors)[0].ErrorContent" 
    RelativeSource="{RelativeSource Self}"> 
+0

我相信它{的RelativeSource自}用空格代替 – Gishu 2010-11-26 12:01:58

相關問題