为什么在添加转换器以突出显示某些关键字后报告不再显示

本文关键字:显示 关键字 不再 报告 添加 转换器 为什么 | 更新日期: 2023-09-27 18:33:07

我添加了转换器,该转换器应该突出显示报告中的一些关键字,但现在报告根本没有显示。如果我从 xaml 代码中删除了转换器,则会显示报告。该代码指示如果未找到关键字,它将只返回原始对象。不知道我做错了什么。

XAML:

 <local:AdvisoryReportView x:Name="_advisoryReportView" Grid.Column="2" Grid.Row="0" Grid.RowSpan="4" 
                              VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
                               DataContext="{Binding AdvisoryViewModels, Converter={StaticResource highlightKeywordsConverter}}"/>

C#

public class HighlightKeywordsConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string returnText = string.Empty; // "Nothing Selected";
        string input = null;
        string replacementText = null;
        string replacePattern = null;
        string[] keyWords = new string[]
        {
                "test1",
                "test2",
                "test3",
        };
        if (value != null && !value.ToString().Contains("DependencyProperty.UnsetValue"))
            input = value.ToString();
            if (!String.IsNullOrEmpty(input))
            {
                for (int i = 0; i < 3; i++)
                {
                    if (input.Contains(keyWords[i]))
                    {
                        switch (i)
                        {
                            case 0:
                                replacementText = String.Format(
                                    "<FONT style='"BACKGROUND-COLOR: Blue'">{0}</FONT>", "$1");
                                replacePattern = @"(?![^<>]*>)(" + keyWords[i] + ")";
                                break;
                            case 1:
                                replacementText = String.Format(
                                    "<FONT style='"BACKGROUND-COLOR: Beige'">{0}</FONT>", "$1");
                                replacePattern = @"(?![^<>]*>)(" + keyWords[i] + ")";
                                break;
                            case 2:
                                replacementText = String.Format(
                                    "<FONT style='"BACKGROUND-COLOR: Azure'">{0}</FONT>", "$1");
                                replacePattern = @"(?![^<>]*>)(" + keyWords[i] + ")";
                                break;                                                                                                                             
                        } //end of switch
                        returnText = Regex.Replace(input,
                                                   replacePattern,
                                                   replacementText,
                                                   RegexOptions.IgnoreCase);
                    } //end of if
                    else return input;
                }//end of for
            } //end of if
            else
            {
                  // Otherwise, just send back the original text, or an 
                  // empty string if we did not get anything.
                  returnText = input;
            }

        return returnText;
    } //end of Convert
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
} //end of class

为什么在添加转换器以突出显示某些关键字后报告不再显示

您的转换器有很多问题。问题的主要原因当然是找不到关键字时 for 循环内的 return 语句,因为它停止查找任何剩余的关键字。并且始终使用 input 变量作为 Replace 方法的第一个参数会导致忽略任何先前进行的替换。

我想这个转换方法就是你所需要的:

public object Convert(
    object value, Type targetType, object parameter, CultureInfo culture)
{
    var returnText = value as string;
    if (!string.IsNullOrEmpty(returnText))
    {
        var keywords = new string[]
        {
            "test1",
            "test2",
            "test3",
        };
        var replacements = new string[]
        {
            "<FONT style='"BACKGROUND-COLOR: Blue'">$1</FONT>",
            "<FONT style='"BACKGROUND-COLOR: Beige'">$1</FONT>",
            "<FONT style='"BACKGROUND-COLOR: Azure'">$1</FONT>"
        };
        for (int i = 0; i < keywords.Length; i++)
        {
            var replacePattern = @"(?![^<>]*>)(" + keywords[i] + ")";
            returnText = Regex.Replace(returnText,
                                       replacePattern,
                                       replacements[i],
                                       RegexOptions.IgnoreCase);
        }
    }
    return returnText;
}