Wpf 根据控件状态设置Validation.ErrorTemplate
我已经修改了基于TextBoxBase控件的默认Validation.ErrorTemplate。 这样我就有了工具提示和弹出的标签,当输入字段获得焦点时显示错误 当控件没有文本但需要输入时,我想删除默认的红色边框,并用控件左侧的星号*替换红色边框 我认为,如果你有很多必填字段,那么使用就会遇到一个充满可怕的红色框的表单。红色边框仅在输入无效值时显示,如年龄等于223 因此,我想我想根据触发器切换模板或部分模板。IMHO您不能在TextBox控件上使用。这是我的错。相反,您可以使用简单的转换器来切换文本框样式 以下是示例代码: 型号:Wpf 根据控件状态设置Validation.ErrorTemplate,wpf,wpf-controls,Wpf,Wpf Controls,我已经修改了基于TextBoxBase控件的默认Validation.ErrorTemplate。 这样我就有了工具提示和弹出的标签,当输入字段获得焦点时显示错误 当控件没有文本但需要输入时,我想删除默认的红色边框,并用控件左侧的星号*替换红色边框 我认为,如果你有很多必填字段,那么使用就会遇到一个充满可怕的红色框的表单。红色边框仅在输入无效值时显示,如年龄等于223 因此,我想我想根据触发器切换模板或部分模板。IMHO您不能在TextBox控件上使用。这是我的错。相反,您可以使用简单的转换器来
public class User : ModelBase
{
private string _login;
[Required(ErrorMessage = "Login can not be empty")]
[MaxLength(20, ErrorMessage = "Login max lenght is 20")]
public string Login
{
get
{
return _login;
}
set
{
_login = value;
OnPropertyChanged("Login");
}
}
}
public class TextBoxStyleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string textBoxTex = value.ToString();
var asteriskErrorStyle = Application.Current.FindResource("AsteriskErrorStyle") as Style;
var redBorderErrorStyle = Application.Current.FindResource("RedBorderErrorStyle") as Style;
if (string.IsNullOrEmpty(textBoxTex))
{
return asteriskErrorStyle;
}
else
{
return redBorderErrorStyle;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<Window.Resources>
<styleSelector:TextBoxStyleConverter x:Key="TextBoxStyleConverter"/>
</Window.Resources>
<TextBox Grid.Column="1"
Grid.Row="0"
Margin="5,5,80,5"
Style="{Binding RelativeSource={RelativeSource Self}, Path=Text,Converter={StaticResource TextBoxStyleConverter}}"
Text="{Binding Path=User.Login, ValidatesOnNotifyDataErrors=True}" >
在App.xaml中使用自定义错误模板创建文本框样式
App.xaml
<Application.Resources>
<Style x:Key="AsteriskErrorStyle"
TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<AdornedElementPlaceholder x:Name="AdornedElementPlaceholder" />
<TextBlock Foreground="Red"
Margin="10,0,0,0"
VerticalAlignment="Top"
FontSize="20"
Text="*"
ToolTip="{Binding ElementName=AdornedElementPlaceholder, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
</TextBlock>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="RedBorderErrorStyle"
TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Border BorderThickness="1.5"
BorderBrush="Red">
<AdornedElementPlaceholder x:Name="AdornedElementPlaceholder" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
用法:
public class User : ModelBase
{
private string _login;
[Required(ErrorMessage = "Login can not be empty")]
[MaxLength(20, ErrorMessage = "Login max lenght is 20")]
public string Login
{
get
{
return _login;
}
set
{
_login = value;
OnPropertyChanged("Login");
}
}
}
public class TextBoxStyleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string textBoxTex = value.ToString();
var asteriskErrorStyle = Application.Current.FindResource("AsteriskErrorStyle") as Style;
var redBorderErrorStyle = Application.Current.FindResource("RedBorderErrorStyle") as Style;
if (string.IsNullOrEmpty(textBoxTex))
{
return asteriskErrorStyle;
}
else
{
return redBorderErrorStyle;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<Window.Resources>
<styleSelector:TextBoxStyleConverter x:Key="TextBoxStyleConverter"/>
</Window.Resources>
<TextBox Grid.Column="1"
Grid.Row="0"
Margin="5,5,80,5"
Style="{Binding RelativeSource={RelativeSource Self}, Path=Text,Converter={StaticResource TextBoxStyleConverter}}"
Text="{Binding Path=User.Login, ValidatesOnNotifyDataErrors=True}" >
您可以下载示例项目。您可以将和属性设置为False
,以停止显示默认的红色边框。您可以使用Validation.ErrorTemplate
属性应用星号,您似乎已经发现:
<ControlTemplate x:Key="RedAsteriskValidationTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="*" Foreground="Red" FontWeight="Bold" Margin="0,0,5,0" />
<AdornedElementPlaceholder />
</StackPanel>
</ControlTemplate>
MSDN上的页面。我当然可以这样做,但我想用我的验证规则驱动UI。对于同一属性,我可以有一个非空规则和一个范围规则。“不为空”应显示星号,“范围”规则应显示边框和带有错误消息的小弹出窗口。