该类型的对象不能应用于需要 system.windows.data.ivalueconverter 类型的属性

本文关键字:类型 windows data 属性 system ivalueconverter 对象 不能 应用于 | 更新日期: 2023-09-27 18:33:05

我创建了一个转换器,用于突出显示报表中不同颜色中的某些关键字。我没有使用IValueconverter,因为关键字是硬编码的。但是,我在 xaml 中得到了错误。我该如何解决:

<ResourceDictionary>
    <converter:HighlightKeywordsConverter x:Key="highlightKeywordsConverter" />
 </ResourceDictionary>
<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
{
    //public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    public object Convert(AdvisoryViewModelCollection input)
    {
        string returnText = string.Empty; // "Nothing Selected";
        string searchText = null;
        string[] keyWords = new string[]
            {
                "CLSD Closed",
                "BA NIL Braking action nil",
                "BA POOR Braking action poor",
            };

                   for (int i = 0; i < 3; i++)
        {
            if (input.ToString().Contains(keyWords[i]))
            {
                switch (i)
                {
                    case 0:
                        searchText = String.Format("<FONT style='"BACKGROUND-COLOR: Blue'">{0}</FONT>", "$1");
                        break;
                    case 1:
                        searchText = String.Format("<FONT style='"BACKGROUND-COLOR: Beige'">{0}</FONT>", "$1");
                        break;
                    case 2:
                        searchText = String.Format("<FONT style='"BACKGROUND-COLOR: Azure'">{0}</FONT>", "$1");
                        break;

                } //end of switch
                if (!String.IsNullOrEmpty(input.ToString()) && !String.IsNullOrEmpty(searchText))
                {
                    string replacePattern = @"(?![^<>]*>)(" + searchText + ")";
                    returnText = Regex.Replace(input.ToString(),
                                               replacePattern,
                                               searchText,
                                               RegexOptions.IgnoreCase);
                }
            } //end of if
            else
            {
                // Otherwise, just send back the original text, or an 
                // empty string if we did not get anything.
                returnText = input.ToString();
            }
        } //end of for
        return returnText;
    } //end of Convert
} //end of class

该类型的对象不能应用于需要 system.windows.data.ivalueconverter 类型的属性

HighlightKeywordsConverter 需要实现IValueConverter