2017-06-21 58 views
4

有沒有一種方法可以在驗證密碼箱時在AdornedElementPlaceholder中顯示錯誤消息。驗證WPF中的密碼框

我有這樣的事情:

<ControlTemplate x:Key="DefaultErrorTemplate"> 
    <StackPanel Orientation="Horizontal"> 
     <AdornedElementPlaceholder x:Name="placeholder" /> 
     <Border Background="Red" 
       ToolTip="{Binding ElementName=placeholder, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" 
       ToolTipService.InitialShowDelay="0" 
       VerticalAlignment="Top" 
       Margin="3" 
       Width="20" 
       Height="20" 
       CornerRadius="10"> 
      <TextBlock Text="!" 
         Foreground="White" 
         FontWeight="Bold" 
         HorizontalAlignment="Center" 
         VerticalAlignment="Center" /> 
     </Border> 
    </StackPanel> 
</ControlTemplate> 

,並使用該驗證

<Style TargetType="Control" 
      x:Key="ControlBaseStyle"> 
     <Setter Property="Validation.ErrorTemplate" 
       Value="{StaticResource DefaultErrorTemplate}" /> 

即時我BaseControlStyle IM和它的工作就像一個魅力與我有(組合框,的DateTimePicker幾乎所有的控制, TextBox),但是當我想使用相同的樣式passwordBox它不起作用。

enter image description here 在圖片中,你可以看到它使用simpe TextBox,但不使用PasswordBox。我不知道如何提取錯誤消息顯示它的提示我的用戶名屬性,AdornedElementPlaceholder

它顯示錯誤消息

[Required(ErrorMessage = "Please enter username.")] 

我wan't實現同樣的事情passwordBox給予反饋用戶輸入密碼時出現錯誤(約束)

任何幫助真的很感激。

感謝提前:)

編輯:

我已經使用這個密碼屬性

[Required(ErrorMessage = "Please enter password.")] 
[RegularExpression(@"^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).*$")] 
[StringLength(maximumLength: 15, ErrorMessage = "Minimum 8 and maximum 15 characters.", MinimumLength = 8)] 
public string Password 
{ 
    get { return GetValue<string>(); } 
    set { SetValue(value); } 
} 

並綁定到該屬性與PasswordBoxAssistant

<PasswordBox behaviors:PasswordBoxAssistant.BindPassword="True" 
      behaviors:PasswordBoxAssistant.BoundPassword="{Binding Player.Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}" 
      FontSize="{StaticResource defaultFontSize}" 
      Grid.Row="2" 
      Grid.Column="1" 
      Margin="10" /> 

,並取得了自定義PasswordBoxStyle即BasedOn ControlBaseStyle

<Style TargetType="{x:Type PasswordBox}" 
     BasedOn="{StaticResource ControlBaseStyle}"> 

我做同樣的事情TextBox,但它不與PasswordBox工作。

信息:我想知道爲什麼你投票結束這個問題?如果對這個甚至是指南有答案,我會很樂意自己關閉它,如果沒有,請在投票結束前給我一個答案來結束它。謝謝。

+0

如何設置密碼框的驗證錯誤?它顯示了我的自定義模板... – grek40

+0

我已經更新了關於如何綁定到密碼的問題,以及我對PasswordBox樣式所做的更改。 –

+0

關於近距離投票:*尋求調試的問題幫助(「爲什麼不是這個代碼工作?「)必須包含所需的行爲,特定的問題或錯誤,以及在問題本身中重現問題所需的最短代碼,沒有明確問題陳述的問題對其他讀者不具有幫助,請參閱:如何創建最小,完整和可驗證的例子*你的例子當然不完整(並且不是) – grek40

回答

0

我找到了答案。 後來換

<Style TargetType="Control" 
     x:Key="ControlBaseStyle"> 
    <Setter Property="Validation.ErrorTemplate" 
      Value="{StaticResource DefaultErrorTemplate}" /> 

,並把它裏面

<Trigger Property="Validation.HasError" 
     Value="True"> 
    <Setter Property="Validation.ErrorTemplate" 
      Value="{StaticResource DefaultErrorTemplate}" /> 

aparently這只是內部觸發時的工作,和TextBox可以工作就像默認和內部觸發。

+0

這真是奇怪......我無法想象有很多原因,我可以想象的少數事情之一是在'behaviour:PasswordBoxAssistant.BindPassword'背後是一個奇怪的實現。如果它在某個地方分配了一個錯誤模板,那麼在普通的setter之前它確實可能優先。 – grek40