/// <summary> /// Convert forward function. /// </summary> /// <param name="value">The source value.</param> /// <param name="targetType">The target type.</param> /// <param name="parameter">The parameter.</param> /// <param name="culture">The culture info.</param> /// <returns>The converted value.</returns> public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Drug drug = value as Drug; if (drug != null) { TextBlock textBlock = new TextBlock(); textBlock.TextWrapping = TextWrapping.Wrap; if (!string.IsNullOrEmpty(drug.Name)) { string[] ingredients = drug.Name.Split("+".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < ingredients.Length; i++) { DrugNameToTextBlockConverter.ReplaceHyphensWithNonBreakingCharacter(textBlock, ingredients[i].Trim(), FontWeights.Bold); if (i < ingredients.Length - 1) { DrugNameToTextBlockConverter.AddCharacterJoin(textBlock); textBlock.Inlines.Add(new Run() { Text = "+ ", FontWeight = FontWeights.Bold }); } } } if (!string.IsNullOrEmpty(drug.Name) && !string.IsNullOrEmpty(drug.BrandName)) { DrugNameToTextBlockConverter.AddCharacterJoin(textBlock); textBlock.Inlines.Add(new Run() { Text = "― " }); } if (!string.IsNullOrEmpty(drug.BrandName)) { string[] parts = drug.BrandName.Split("+".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < parts.Length; i++) { DrugNameToTextBlockConverter.ReplaceHyphensWithNonBreakingCharacter(textBlock, parts[i].Trim(), FontWeights.Normal); if (i < parts.Length - 1) { DrugNameToTextBlockConverter.AddCharacterJoin(textBlock); textBlock.Inlines.Add(new Run() { Text = "+ ", }); } } } return(textBlock); } return(null); }
/// <summary> /// Adds inlines for text that will not break unless necessary. /// </summary> /// <param name="textBlock">The text block to add the inlines too.</param> /// <param name="text">The text to add.</param> /// <param name="fontWeight">The desired font weight of the text.</param> private static void AddNonBreakingInlines(TextBlock textBlock, string text, FontWeight fontWeight) { // Split text into parts around a long dash // e.g. oral ― modified-release becomes "oral", "modified-release" string[] parts = text.Split("―".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (text.StartsWith("― ", StringComparison.CurrentCulture)) { textBlock.Inlines.Add(new Run() { Text = "― ", FontWeight = fontWeight, }); } for (int p = 0; p < parts.Length; p++) { // Split words by space, string[] words = parts[p].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < words.Length; i++) { DrugNameToTextBlockConverter.ReplaceHyphensWithNonBreakingCharacter(textBlock, words[i], fontWeight); if (i != words.Length - 1) { AddCharacterJoin(textBlock); } } if (p != parts.Length - 1) { AddCharacterJoin(textBlock); textBlock.Inlines.Add(new Run() { Text = "― " }); } } }