我需要检查在该字段中输入的数据。对于TextBox
,这很简单:
XML代码
<Window.Resources>
<ControlTemplate x:Key="validationTemplate">
<DockPanel>
<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
<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>
</Window.Resources>
<TextBox x:Name="loginTextBox" Validation.ErrorTemplate="{StaticResource validationTemplate}" Style="{StaticResource textBoxInError}"
HorizontalAlignment="Left" Height="23" Margin="135,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="250">
<TextBox.Text>
<Binding Path="Login" >
<Binding.ValidationRules>
<local:ValidationRuleModel />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
课程代码ValidationRuleModel
public class ValidationRuleModel : ValidationRule
{
public ValidationRuleModel()
{
}
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
try
{
if (string.IsNullOrEmpty((string)value))
{
return new ValidationResult(false, "Enter the data in the field");
}
else
{
return ValidationResult.ValidResult;
}
}
catch (Exception e)
{
return new ValidationResult(false, "Illegal characters or " + e.Message);
}
}
}
但是PasswordBoxes
怎么做呢。由于安全原因和{/ p>,Password
不可绑定
<PasswordBox.Password>
<Binding Path="Password" >
<Binding.ValidationRules>
<local:ValidationRuleModel />
</Binding.ValidationRules>
</Binding>
</PasswordBox.Password>
无法完成。我尝试使用here和here中的提示,但没有任何反应。
那么,如何检查用户是否在PasswordBox字段中输入了数据?
答案 0 :(得分:1)
您可以使用this PasswordBoxAssistant class:
<PasswordBox local:PasswordBoxAssistant.BindPassword="True">
<local:PasswordBoxAssistant.BoundPassword>
<Binding Path="Password" Mode="TwoWay">
<Binding.ValidationRules>
<local:ValidationRuleModel />
</Binding.ValidationRules>
</Binding>
</local:PasswordBoxAssistant.BoundPassword>
</PasswordBox>