HTML  Convert

In this case both properties are of type string, so the converter trick is not really needed. However, here we use the converter to strip out HTML markup and clean up the text to display. Again, the converter class needs to be declared as a resource, as shown above.

public class HtmlSanitizer : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   {
// Remove HTML tags and empty newlines and spaces
string returnString = Regex.Replace(value as string, "<.*?>", "");
      returnString = Regex.Replace(returnString, @"\n+\s+", "\n\n");

// Decode HTML entities
returnString = HttpUtility.HtmlDecode(returnString);

return returnString;
   }

   public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   {
throw new NotImplementedException();
   }
}

+ Recent posts