文本框中的输入验证
Input Validation in TextBox
不希望在 start/as 选项卡打开时显示错误消息。
验证是使用 IDataErrorInfo(对 VM)实现的,定义了一个 ErrorTemplate (XAML) 并且能够在 TextBox 旁边获取错误消息。
VM:(extended IDataErrorInfo)
public string this[string propertyName]
{
get
{
string validationResult = null;
switch (propertyName)
{
case "ProjectManager":
validationResult = ValidateManagerName();
break;
}
return validationResult;
}
}
public string ValidateManagerName()
{
if (string.IsNullOrEmpty(this.ProjectManager))
{
return "Manager name is mandatory!";
}
else
return string.Empty;
}
XAML.cs
save_option_summ()
{
BindingExpression be3 = managername.GetBindingExpression(TextBox.TextProperty);
be3.UpdateSource();
}
<StackPanel.Resources>
<ControlTemplate x:Key="ErrorTemplate">
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Right" Foreground="Blue" FontSize="13" Text="{Binding ElementName=adorned,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" >
</TextBlock>
<Border BorderBrush="Red" BorderThickness="2">
<AdornedElementPlaceholder x:Name="adorned"/>
</Border>
</DockPanel>
</ControlTemplate>
</StackPanel.Resources>
<StackPanel Orientation="Horizontal" Margin="0,5,0,0">
<Label Content="Project Manager" FontSize="14" Margin="100,0,0,0" Width="150" FontFamily="Calibri"></Label>
<TextBox Height="auto" Width="300" Background="White" Margin="100,0,0,0" Validation.ErrorTemplate = "{StaticResource ResourceKey=ErrorTemplate}" Name="managername" Text="{Binding ProjectManager,Mode=TwoWay,UpdateSourceTrigger=LostFocus,ValidatesOnDataErrors=True}" TextChanged="TextChanged" FontFamily="Calibri" FontSize="14"/>
</StackPanel>
您应该修改 TextBox 样式,使其显示 属性 有什么问题。这是一个将错误显示为工具提示的简单示例:
<TextBox>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors).[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
因此,连同 shivam sood(有助于在工具提示中显示错误)的答案。
在上面显示的代码(问题)中,
public string ValidateManagerName()
{
if (string.IsNullOrEmpty(this.ProjectManager))
{
return "string.Empty";
}
//Other conditions can be written like alphanumeric check
else
return string.Empty;
}
不要return空检查(第一个条件)中的任何内容,这样文本框就不会总是显示错误消息。
不希望在 start/as 选项卡打开时显示错误消息。
验证是使用 IDataErrorInfo(对 VM)实现的,定义了一个 ErrorTemplate (XAML) 并且能够在 TextBox 旁边获取错误消息。
VM:(extended IDataErrorInfo)
public string this[string propertyName]
{
get
{
string validationResult = null;
switch (propertyName)
{
case "ProjectManager":
validationResult = ValidateManagerName();
break;
}
return validationResult;
}
}
public string ValidateManagerName()
{
if (string.IsNullOrEmpty(this.ProjectManager))
{
return "Manager name is mandatory!";
}
else
return string.Empty;
}
XAML.cs
save_option_summ()
{
BindingExpression be3 = managername.GetBindingExpression(TextBox.TextProperty);
be3.UpdateSource();
}
<StackPanel.Resources>
<ControlTemplate x:Key="ErrorTemplate">
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Right" Foreground="Blue" FontSize="13" Text="{Binding ElementName=adorned,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" >
</TextBlock>
<Border BorderBrush="Red" BorderThickness="2">
<AdornedElementPlaceholder x:Name="adorned"/>
</Border>
</DockPanel>
</ControlTemplate>
</StackPanel.Resources>
<StackPanel Orientation="Horizontal" Margin="0,5,0,0">
<Label Content="Project Manager" FontSize="14" Margin="100,0,0,0" Width="150" FontFamily="Calibri"></Label>
<TextBox Height="auto" Width="300" Background="White" Margin="100,0,0,0" Validation.ErrorTemplate = "{StaticResource ResourceKey=ErrorTemplate}" Name="managername" Text="{Binding ProjectManager,Mode=TwoWay,UpdateSourceTrigger=LostFocus,ValidatesOnDataErrors=True}" TextChanged="TextChanged" FontFamily="Calibri" FontSize="14"/>
</StackPanel>
您应该修改 TextBox 样式,使其显示 属性 有什么问题。这是一个将错误显示为工具提示的简单示例:
<TextBox>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors).[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
因此,连同 shivam sood(有助于在工具提示中显示错误)的答案。
在上面显示的代码(问题)中,
public string ValidateManagerName()
{
if (string.IsNullOrEmpty(this.ProjectManager))
{
return "string.Empty";
}
//Other conditions can be written like alphanumeric check
else
return string.Empty;
}
不要return空检查(第一个条件)中的任何内容,这样文本框就不会总是显示错误消息。