2011-05-20 289 views
0

所以我想沿着這個例子做一些事情:http://www.codeproject.com/KB/WPF/wpfvalidation.aspx自定義驗證

我的文本框目前看起來是這樣的:

<TextBox Height="23" HorizontalAlignment="Left" Margin="118,60,0,0" Name="CreateUserCPRTextbox" VerticalAlignment="Top" Width="120" > 
       <TextBox.Text> 

        <Binding Path="Name" UpdateSourceTrigger="LostFocus"> 
         <Binding.ValidationRules> 
          <validators:TextRangeValidator 
         MinimumLength="10" 
         MaximumLength="10" 
         ErrorMessage="ID has to be 10 letters" /> 
         </Binding.ValidationRules> 
        </Binding> 
       </TextBox.Text> 

      </TextBox> 

我直接複製我的TextRangeValidator從示例在該網站上。當我失去對文本框的關注時,什麼也沒有發生。不管我輸入什麼。有任何想法嗎? :)

回答

2

您已設置的Validation.ErrorTemplate?它是在example.You的Application.Resources定義如下可能已經錯過了

<Application.Resources> 

     <Style TargetType="{x:Type TextBox}"> 
      <Setter Property="Validation.ErrorTemplate"> 
       <Setter.Value> 
        <ControlTemplate> 
         <DockPanel LastChildFill="True"> 

          <TextBlock DockPanel.Dock="Right" 
           Foreground="Orange" 
           Margin="5" 
           FontSize="12pt" 
           Text="{Binding ElementName=MyAdorner, 
          Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"> 
          </TextBlock> 

          <Border BorderBrush="Green" BorderThickness="3"> 
           <AdornedElementPlaceholder Name="MyAdorner" /> 
          </Border> 

         </DockPanel> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
      <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> 

    </Application.Resources> 

編輯

您的默認值不會觸發驗證routine.To迫使它來驗證默認珍惜你必須設置

<validators:TextRangeValidator ValidatesOnTargetUpdated="True" 
         MinimumLength="10" 
         MaximumLength="10" 
         ErrorMessage="ID has to be 10 letters" /> 
+0

我沒有這樣做,因爲我認爲它會使用默認值。它在哪裏使文本框的邊框變紅?我不確定在哪裏適合這個代碼 - 這就是爲什麼我沒有使用它嘿......我是全新的WPF。 – 2011-05-20 12:10:39

+0

實際上應該會出現默認的紅色邊框。但它不會顯示您的錯誤消息 – biju 2011-05-20 12:14:07

+0

應該去的地方?我把它放在裏面? – 2011-05-20 12:20:28

1

我認爲你需要設置ValidatesOnDataErrors=True在文本結合,使其工作

+0

似乎沒有文本框獲得該屬性?或者你會在哪裏添加它? – 2011-05-20 12:04:13

+0

說得更清楚我認爲 – 2011-05-20 12:09:47

+0

這沒有奏效。任何其他想法? :) – 2011-05-20 12:20:52

0

WPF的TextBox驗證

<Style x:Key="TextBoxInError" TargetType="TextBox"> 
     <Setter Property="Validation.ErrorTemplate"> 
      <Setter.Value> 
       <ControlTemplate> 
        <DockPanel>       

         <Grid> 
          <Polygon Points="20,10,20,0 0,0" 
          Stroke="Black" 
          StrokeThickness="1" 
          Fill="Red" 
          HorizontalAlignment="Right" 
          VerticalAlignment="Top" 
          ToolTip="{Binding ElementName=adorner, 
           Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/> 
          <AdornedElementPlaceholder x:Name="adorner"/> 


         <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" > 
          <Border BorderBrush="red" BorderThickness="1" /> 
         </AdornedElementPlaceholder> 


         </Grid> 

         <Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" 
          Width="150" Height="20" CornerRadius="5" 
          ToolTip="{Binding ElementName=customAdorner, 
             Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"> 
          <TextBlock Text= "{Binding ElementName=customAdorner, 
             Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" VerticalAlignment="center" HorizontalAlignment="Left" 
           FontWeight="Bold" Foreground="white" Width="250" />        


         </Border> 


        </DockPanel> 



       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="Validation.HasError" Value="True"> 
       <Setter Property="ToolTip" 
        Value="{Binding RelativeSource={x:Static RelativeSource.Self}, 
       Path=(Validation.Errors)[0].ErrorContent}" /> 

      </Trigger> 
     </Style.Triggers>   


    </Style> 
+0

你需要說明你已經做了什麼來給出一些關於這是一個答案的指導,而不僅僅是郵政編碼 – 2013-03-19 10:21:39