如何使用IDataerroInfo接口在richtextbox中执行验证?

时间:2019-06-26 18:51:52

标签: c# wpf idataerrorinfo

我正在尝试在Richtextbox上实现Idataerrorinfo接口以进行自定义验证。当我遵循相同的模式为richtextbox实现时,我成功地为textbox实现了它。

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}"/>

Viewmodel.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时,周围什么都没有发生,而且我不确定如何找到问题所在。

0 个答案:

没有答案