我试图在文本框为空时将其边框设为红色。这是我的 xaml:
<TextBox Style="{StaticResource TextBoxEmptyError}" Name="tbFilename" Grid.Column="1" >
<Binding Path="Text" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
<Binding.ValidationRules>
<local:EmptyRule />
</Binding.ValidationRules>
</Binding>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
我正在尝试设置的样式:
<Style x:Key="TextBoxEmptyError" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="BorderThickness" Value="2" />
<Setter Property="BorderBrush" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
空规则:
public class EmptyRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (string.IsNullOrEmpty(value as string))
return new ValidationResult(false, null);
else
return new ValidationResult(true, null);
}
}
Run Code Online (Sandbox Code Playgroud)
在调试器中,看起来根本没有使用 Validation 方法。我究竟做错了什么?