RichTextBox in WPF does not have an property as .Lines?
RichTextBox is a FlowDocument type and that does not have a Lines property. What you are doing seems like a good solution. You may want to use IndexOf instead of split.
You can also add an extension method like the article suggests:
public static long Lines(this string s)
{
long count = 1;
int position = 0;
while ((position = s.IndexOf('\n', position)) != -1)
{
count++;
position++; // Skip this occurance!
}
return count;
}
How to get a WPF rich textbox into a string
At the bottom of the MSDN RichTextBox reference there's a link to How to Extract the Text Content from a RichTextBox
It's going to look like this:
public string RichTextBoxExample()
{
RichTextBox myRichTextBox = new RichTextBox();
// Create a FlowDocument to contain content for the RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();
// Add initial content to the RichTextBox.
myRichTextBox.Document = myFlowDoc;
// Let's pretend the RichTextBox gets content magically ...
TextRange textRange = new TextRange(
// TextPointer to the start of content in the RichTextBox.
myRichTextBox.Document.ContentStart,
// TextPointer to the end of content in the RichTextBox.
myRichTextBox.Document.ContentEnd
);
// The Text property on a TextRange object returns a string
// representing the plain text content of the TextRange.
return textRange.Text;
}
How to get all text from RichTextBox in WPF
If you don't need the multiline you can disable it and shouldn't have this problem.
You could also remove them like this:
string text = textRange.Text.Replace(Environment.NewLine, "");
Also if you only want to remove the last ocurrence you can use a function like this:
public static string ReplaceLastOccurrence(string Source, string Find, string Replace)
{
int Place = Source.LastIndexOf(Find);
string result = Source.Remove(Place, Find.Length).Insert(Place, Replace);
return result;
}
And use it like this.
string text = ReplaceLastOccurrence(textRange.Text,Environment.NewLine,"");
Every time you have a you will get a New Line at the end, so make sure you handle it acordingly.
RichTextBox WPF limit content in one line
With thanks to mm8, his answer gave me a direction. But I needed a solution to bind to a collection from the ViewModel. And the number of bold areas in string are decided at runtime. So, I have created a dependency property. And following worked for me. Sharing for benefit of others in future.
TextBlockExtensions.cs
public class TextBlockExtensions
{
public static IEnumerable<Inline> GetBindableInlines(DependencyObject obj)
{
return (IEnumerable<Inline>)obj.GetValue(BindableInlinesProperty);
}
public static void SetBindableInlines(DependencyObject obj, IEnumerable<Inline> value)
{
obj.SetValue(BindableInlinesProperty, value);
}
public static readonly DependencyProperty BindableInlinesProperty =
DependencyProperty.RegisterAttached("BindableInlines", typeof(IEnumerable<Inline>), typeof(TextBlockExtensions), new PropertyMetadata(null, OnBindableInlinesChanged));
private static void OnBindableInlinesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var Target = d as TextBlock;
if (Target != null)
{
Target.Inlines.Clear();
Target.Inlines.AddRange((System.Collections.IEnumerable)e.NewValue);
}
}
}
In my ViewModel, I added,
private ObservableCollection<Inline> _searchSuggestionInlines;
public ObservableCollection<Inline> SearchSuggestionInlines
{
get
{
return _searchSuggestionInlines;
}
set
{
SetProperty(ref _searchSuggestionInlines, value, nameof(SearchSuggestionInlines));
}
}
//To populate the inlines
public SearchSuggestionViewModel(Suggestion suggestion)
{
_suggestion = suggestion;
if (string.IsNullOrEmpty(suggestion.Text))
return;
string searchText = _suggestion.Text;
ObservableCollection<Inline> inlines = new ObservableCollection<Inline>();
Run run = new();
while (searchText.Contains("<b>"))
{
string initialText = searchText.Substring(0, searchText.IndexOf("<b>"));
run.Text = initialText;
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
{
FontWeight = FontWeights.Bold,
Text = boldText
};
inlines.Add(run);
searchText = searchText.Substring(searchText.IndexOf("</b>") + "</b>".Length);
}
run = new Run
{
Text = searchText
};
inlines.Add(run);
SearchSuggestionInlines = inlines;
}
While the following was added to View,
<TextBlock controls:TextBlockExtensions.BindableInlines="{Binding Path=SearchSuggestionInlines, Mode=OneWay}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis" ToolTip="{Binding Path=SearchSuggestionText, Mode=OneWay}"/>
Regards,
Umar
C# String in RichTextBox Wpf
yourRichTextBox.AppendText(BodyString);
Related Topics
A Field Initializer Cannot Reference the Non-Static Field, Method, or Property
Executenonquery: Connection Property Has Not Been Initialized
How to Dynamically Create ASP.NET Controls Within Dynamically Created ASP.NET Controls
Arrays, Heap and Stack and Value Types
C# If/Then Directives for Debug VS Release
How to Create/Edit a Manifest File
Change Attribute's Parameter at Runtime
How to Bind an Enum to a Combobox Control in Wpf
Correct Use of Multimapping in Dapper
Microsoft.Jet.Oledb.4.0' Provider Is Not Registered on the Local MAChine
Calling Generic Method with a Type Argument Known Only at Execution Time
Execute Multiple Command Lines with the Same Process Using .Net
Equivalent to 'App.Config' for a Library (Dll)
Best Way to Access a Control on Another Form in Windows Forms