【发布时间】:2020-12-06 08:23:06
【问题描述】:
在使用验证规则时,如何为自动应用于控件的默认工具提示制作自定义模板?还有有没有办法改变这个工具提示的持续时间?
我的意思是这个工具提示:
XAML:
<Window.Resources>
<!--Validation Template-->
<ControlTemplate x:Key="MyErrorTemplate">
<Border BorderBrush="#e92539" BorderThickness="2" CornerRadius="10">
<Grid>
<AdornedElementPlaceholder/>
</Grid>
</Border>
</ControlTemplate>
</Window.Resources>
<TextBox FontSize="13" Validation.ErrorTemplate="{StaticResource MyErrorTemplate}"
AcceptsReturn="False" Panel.ZIndex="3" x:Name="txtName"
Style="{StaticResource MyTextBox}" Grid.Row="2">
<TextBox.Text>
<Binding Path="txtName" Mode="TwoWay"
UpdateSourceTrigger="PropertyChanged"
ValidatesOnDataErrors="True">
<Binding.ValidationRules>
<local:ValidateTextBox/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
C#:
class ValidateTextBox : ValidationRule
{
Regex regex = new Regex(@"[\\/:*?""<>|]");
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
string txtNameText = value as string;
if (regex.IsMatch(txtNameText))
{
return new ValidationResult(false, "A file name can't contain any of the following characters:\n \\ / : * ? \" < > |");
}
else
{
return new ValidationResult(true, null);
}
}
}
【问题讨论】:
标签: c# wpf validation xaml controltemplate