c# - 如何使用 IDataerroInfo 接口在 Richtextbox 中执行验证?
问题描述
我正在尝试在 Richtextbox 上实现 Idataerrorinfo 接口以进行自定义验证。当我按照相同的模式为richtextbox实现时,我成功地为一个文本框实现了它,在richtextbox周围没有任何反应。
windows.xaml:
<Window.Resources>
<ControlTemplate x:Key="ErrorTemplate">
<DockPanel LastChildFill="True">
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder />
</Border>
</DockPanel>
</ControlTemplate>
<Style TargetType="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>
<Style TargetType="RichTextBox">
<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>
<TextBlock Grid.Column="0" Text="Comments" HorizontalAlignment="Center" VerticalAlignment="Center" Width="70" />
<RichTextBox Grid.Column="1">
<FlowDocument PageHeight="180">
<Paragraph>
<Run Text="{Binding informationdata.Usercomments, Mode=TwoWay, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Validation.ErrorTemplate="{StaticResource ErrorTemplate}"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
<TextBlock Grid.Column="5" Text="Time Taken" HorizontalAlignment="Center" VerticalAlignment="Center" Width="79" />
<TextBox Grid.Column="6" IsEnabled="{Binding informationdata.Isdisabled}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="99" Height="19" Text="{Binding informationdata.Timetaken, Mode=TwoWay, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Validation.ErrorTemplate="{StaticResource ErrorTemplate}"/>
视图模型.cs:
private string GetValidationError(string propertyName)
{
string errorMsg = null;
switch (propertyName)
{
case "Timetaken":
if ((propertyName.Equals("Timetaken")))
{
if (String.IsNullOrEmpty(this.Timetaken))
errorMsg = "Please provide the TimeTaken";
else if (CheckInteger(Timetaken) == false)
errorMsg = "The given TimeTaken is not a Number";
else if (Timetaken.Length > 480)
errorMsg = "The TimeTaken in mins should be less than 480 Mins";
}
break;
case "Usercomments":
if ((propertyName.Equals("Usercomments")))
{
if (String.IsNullOrEmpty(this.Usercomments))
errorMsg = "Please provide the User comments";
}
break;
}
return errorMsg;
}
public string this[string columnName]
{
get
{
return GetValidationError(columnName);
}
}
在上面的代码中,文本框验证工作正常,但是对于richtextbox,它周围没有任何反应,我不确定如何找到它有什么问题。
解决方案
推荐阅读
- python - Python 3 等效于 Python 2 urllib.urlencode
- python - 执行脚本时不显示绘图
- sql - 为什么选择 * 只返回组中的第一条记录?
- java - 为什么在HashMap中输入“i”而不是“j”作为键时输出不同?
- laravel - How to use Eloquent relationships with Yajra Datatables?
- python - 文本显示在调试窗口而不是 Kivy App 的弹出窗口中
- razor-pages - 如何获取剃刀页面中的所有选择列表项?
- android - 如何在 Xamarin Android 中使用 Facebook Audience Network 对 Admob 中介进行故障排除/测试?
- automation - 如何在 Maximo 自动化脚本中正确添加人工交易记录
- clickhouse - 如何理解 ClickHouse 中的粒度和块?