2013-09-26 76 views
0

我創建了一個轉換器,用於在報表中突出顯示不同顏色的某些關鍵字。我沒有使用IValueconverter,因爲關鍵字是硬編碼的。但是,我在xaml中遇到了錯誤。如何解決這個問題呢:該類型的對象不能應用於需要類型的屬性。system.windows.data.ivalueconverter

<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 
+0

轉換器需要接收實現IValueConverter接口的資源... Convert方法的參數值將具有您想要的AdvisoryViewModelCollection! – sexta13

回答

4

HighlightKeywordsConverter需要實現IValueConverter

+0

如何定義突出顯示所需的硬編碼文本的值?這些不通過值參數傳遞。 –

+0

你有兩種可能性。或者保留硬編碼的值,或者使用這些關鍵字定義一個StaticResource綁定到模型中的列表,並將其傳遞給ConverterParameter。 – sexta13

+0

謝謝!它有助於! –

相關問題