2013-10-19 99 views
1

我已經構建了一個表單來編輯我的WPF應用程序中的數據。我正在爲表單添加驗證。我從this articlethis one開始,但是錯誤模板或者全部顯示,或者根本不顯示。我不知道我做錯了什麼。我正在使用的是ControlTemplateStyle。他們是在窗體的資源:TextBox驗證不顯示錯誤

<ControlTemplate x:Key="TextBoxErrorTemplate"> 
    <StackPanel ClipToBounds="False" Orientation="Horizontal"> 
     <Border BorderBrush="Red" 
       BorderThickness="1" 
       Margin="15,0,0,0"> 
      <AdornedElementPlaceholder Name="adornedElement" /> 
     </Border> 
     <Image HorizontalAlignment="Right" 
       VerticalAlignment="Top" 
       Width="20" 
       Height="20" 
       Margin="0,-5,-5,0" 
       Source="{StaticResource ErrorImage}" 
       ToolTip="{Binding Converter={StaticResource ErrorConverter}, 
           ElementName=adornedElement, 
           Path=AdornedElement.(Validation.Errors)}" /> 
    </StackPanel> 
</ControlTemplate> 

<Style x:Key="TextBoxErrorStyle" TargetType="{x:Type TextBox}"> 
    <Style.Triggers> 
     <Trigger Property="Validation.HasError" Value="true"> 
      <Setter Property="ToolTip" 
        Value="Binding Converter={StaticResource ErrorConverter}, 
            RelativeSource={x:Static RelativeSource.Self}, 
            Path=AdornedElement.(Validation.Errors)}"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

這裏是一個使用這些部件的TextBox

<TextBox Grid.Column="0" 
      Margin="5,0" 
      MaxLength="50" 
      Name="NameBox" 
      TabIndex="0" 
      Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}" 
      Style="{StaticResource TextBoxErrorStyle}" 
      TextAlignment="Left" 
      TextChanged="NameBox_TextChanged" 
      VerticalAlignment="Center" 
      Visibility="{Binding Converter={StaticResource InvertedBoolToVisibility}, Path=AutoConfigureCameras, RelativeSource={RelativeSource AncestorType={x:Type cs:EditLPRDetails}}}"> 
    <TextBox.Text> 
     <Binding Mode="TwoWay" Path="Name" UpdateSourceTrigger="PropertyChanged"> 
      <Binding.ValidationRules> 
       <cs:RegexValidationRule Pattern="{StaticResource NamePattern}" /> 
      </Binding.ValidationRules> 
     </Binding> 
    </TextBox.Text> 
</TextBox> 

注意的是,在RegexValidationRule類作品中的驗證邏輯。當我將一個有效的字符串放入TextBox時,它會返回成功,並且當我將一個無效字符串放入它時,它將返回失敗。無論怎樣,我認爲問題出在Style's Trigger

回答

0

我找到了答案,我的問題。

事實證明,我的對話框包含一個TabControl,這是問題的原因。我在this article找到了答案。基本上,我需要把TabItem的內容包含控件在AdornerDecorator控件中進行驗證,該控件本身位於Border控件的內部。一旦完成,那麼錯誤指示符都會正確顯示。

我沒有包括我的控件起初在TabControl之內的事實,因爲我不知道它的重要性。活到老,學到老。

+0

該死的tabcontrol ...解決方案的榮譽!!! – Nitin

2

你是很近,SetterValue語法結合是不正確的,再加上你應該設置PathValidation.Errors

<Setter Property="ToolTip" 
     Value="{Binding Converter={StaticResource ErrorConverter}, 
         RelativeSource={x:Static RelativeSource.Self}, 
         Path=(Validation.Errors)}"/> 
+0

我錯過了左開大括號。我完全錯過了,甚至沒有看到它。現在,工具提示出現並正確讀取,但紅色邊框和位圖仍然不顯示 –

+0

,那麼問題出在綁定屬性上。您可以檢查在Textbox的DataContext中是否正確定義了Name屬性? – Nitin

+0

DataContext設置在'Window'級別。 TextBox的Text屬性綁定到ViewModel對象的Name屬性。當它顯示一個已存在的對象時,正確的值顯示在「TextBox」中,這樣​​'Binding'是正確的。我在'ToolTip Binding'中使用的'IValueConverter'中放置了一個斷點,並且我已經看到該斷點被擊中。我不確定你在說什麼綁定。 –