I need to have parts of the string in bold. Since TextBlock does not support having parts of the text in bold, I moved to using RichTextBox. Now, I want my RichTextBox to limit to a single line and if the contents are longer to fit in single line, it should use character ellipsis to truncate the string. Following is my ViewModel bound to RichTextBox,
public class SearchSuggestionViewModel : BindableBase, IComparable<SearchSuggestionViewModel>
{
private Suggestion _suggestion;
private string m_DocumentXaml = string.Empty;
public SearchSuggestionViewModel(Suggestion suggestion)
{
_suggestion = suggestion;
if (string.IsNullOrEmpty(suggestion.Text))
return;
string searchText = _suggestion.Text;
FlowDocument document = new FlowDocument();
Paragraph paragraph = new Paragraph();
Run run = new Run();
while (searchText.Contains("<b>"))
{
string initialText = searchText.Substring(0, searchText.IndexOf("<b>"));
run.Text = initialText;
paragraph.Inlines.Add(run);
searchText = searchText.Substring(searchText.IndexOf("<b>") + "<b>".Length);
string boldText = searchText;
if (searchText.Contains("</b>"))
boldText = searchText.Substring(0, searchText.IndexOf("</b>"));
run = new Run();
run.FontWeight = FontWeights.Bold;
run.Text = boldText;
paragraph.Inlines.Add(run);
searchText = searchText.Substring(searchText.IndexOf("</b>") + "</b>".Length);
}
run = new Run();
run.Text = searchText;
paragraph.Inlines.Add(run);
document.Blocks.Add(paragraph);
DocumentXaml = XamlWriter.Save(document);
}
public string Id
{
get
{
return _suggestion.Id;
}
}
public string SearchSuggestionText
{
get
{
if (!string.IsNullOrWhiteSpace( _suggestion.Text))
return _suggestion.Text.Replace("<b>", "").Replace("</b>", "");
return string.Empty;
}
}
/// <summary>
/// The text from the FsRichTextBox, as a XAML markup string.
/// </summary>
public string DocumentXaml
{
get
{
return m_DocumentXaml;
}
set
{
SetProperty(ref m_DocumentXaml, value, nameof(DocumentXaml));
}
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override bool Equals(object obj)
{
if (!(obj is SearchSuggestionViewModel))
return false;
SearchSuggestionViewModel otherTag = (SearchSuggestionViewModel)obj;
if (SearchSuggestionText.Equals(otherTag.SearchSuggestionText))
return true;
return false;
}
public int CompareTo(SearchSuggestionViewModel compareSearchSuggestionViewModel)
{
// A null value means that this object is greater.
if (compareSearchSuggestionViewModel == null)
return 1;
else
return this.SearchSuggestionText.CompareTo(compareSearchSuggestionViewModel.SearchSuggestionText);
}
public override string ToString()
{
return _suggestion.Text;
}
}
Any suggestions on how can I achieve to have character ellipsis before the line end.
Regards, Umar