首页 > 解决方案 > 如何使用 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,它周围没有任何反应,我不确定如何找到它有什么问题。

标签: c#wpfidataerrorinfo

解决方案


推荐阅读