I need to delete the last line of RichTextBox in WPF, to remove the item
A RichTextBox doesn't work on a line-by-line basis, but on flow content.
To quote from MSDN:
Specifically, the content edited in a RichTextBox is flow content.
Flow content can contain many types of elements including formatted
text, images, lists, and tables. See Flow Document Overview for in
depth information on flow documents. In order to contain flow content,
a RichTextBox hosts a FlowDocument object which in turn contains the
editable content.
But, to get and manipulate content, I'd suggest taking a look at some of the examples at MSDN. Something there should fit what you're dealing with.
From the comments on Aaron Thomas' answer, the problem was solved with the following code:
myRichTextBox.Document.Blocks.Remove(myRichTextBox.Document.Blocks.LastBlock);
Related
I'm making a text editor with WPF. I'm using a FlowDocument element wrapped in a RichTextBox. My goal is to add extra information to each new paragraph that is created at runtime (see example). As in, each time the user creates a new paragraph, this extra information is displayed.
<RichTextBox x:Name="richText">
<FlowDocument IsOptimalParagraphEnabled="True"
IsHyphenationEnabled="True"
Name="flowDoc">
<!--set it's "paragraph template" here and
just bind all of the FlowDocument text to a
property in my ViewModel-->
</FlowDocument>
<RichTextBox>
The first "extra information" would be the number of the paragraph. The same way code editors specify the line number, I want the user to be able to see the paragraph number.
The second "extra information" would be a set of stats about the paragraph. Be it, world count, estimated reading time, etc etc etc.
--
At first, my plan was to create a flowdocument, set it's paragraph template to a grid with a textblock for the paragraph number, another one for the stats, and so on. But from what I've gather, this is not possible.
(Coming from ListViews, ListBoxes etc, I'm used to be able to set a template to the children of this controls. Almost as using an ForEach loop.)
I'm looking for a way to accomplish what's in the linked picture, the best idea I have is to, somehow, access the paragraphs positions and some other control generate the "extra information".
Is this possible? If so, any suggestion?
Is there a better way?
Thank you. Any idea would be greatly appreciated.
I need to make a sort of text processor. It must have these features:
All the WordPad features + paragraph styles like MS Word + text
columns + Zoom.
Insertion of tables, images, and controls like buttons, checkboxes.
Text search and replacement, spell check.
Syntax coloring, like code or quote parts on the StackOverflow site.
Ability to move text paragraph on custom places via mouse (like OneNote)
Ability to add some simple diagram (hierarchic)
I found on the web that the best way to implement the above requirements is to use FlowDocument. I also found that WPF RichTextControl would have many required functionalities, but on StackOverflow it is written that it has bad performance.
So, does it exist a FlowDocument text processor control?
If not, from which control could I start in order to implement the above requirements?
I'm currently spiking with the WPF RichTextBox before I decide whether or not it can be used in a project of mine.
What I need is to have elements of text representing various objects (other texts or objects), a bit like a WIKI but not quite. Clicking on such a text will make stuff happen, like navigating to other texts or providing additional options.
Anyway, as these little text bits represent other objects I would like to protect them but I have succeeded with this only in part: The user cannot position a caret inside such a text element and edit/delete it but it is still possible to make a selection and delete/replace it, including my custom elements.
Have anyone travelled down this road with the RichTextBox? My latest experiment was to simply record all custom text elements when being part of a selection and then restoring them after the (destructive) edit. That fell apart because I can't find a way to re-insert my custom inline elements (derived from the Run class). The only way I've found to programmatically insert a Run (based) element at a specified position (TextPosition) is via its constructor.
Well, any hints would be greatly appreciated.
You are really looking for a FlowDocument, not a RichTextBox.
I am looking to bind a richtextbox's text property to an array or list of custom objects. Is this possible? I see lots of WPF related binding for richtextbox but I am doing this in winforms. My searches for an example article have not turned up anything of value yet. Figured I would ask here.
One of my database calls returns to me a list of specific instructions and I would like to stuff this entire list of instructions into the richtextbox all at once. The text property does not support an IEnumerable type so I am not sure if I can do what I am trying to do or not. I was looking for a method to fill my richtextbox without all the for looping and inserting one item at a time. I figured data binding was the best fit for that.
I've run across the same problem and have solved it by creating a custom ValueConverter. The code for it can be found here.
Basically it takes an IEnumerable in and then returns a string with a custom separator (defaults to NewLine).
I understand and have read about using WPF's FlowDocument to create an XML style document on screen, but is the content presented editable by the user, or is it read only? And, if so, how is this done?
My question mostly centres around the use of the listitem control because it would be nice to be able to edit the order of the list items presented for use in my program, rather than me having to create my own custom control(s).
Regards.
FlowDocuments as objects are editable, if they can be edited in the UI depends on the controls that use them. If you use them in a Page they are not, if you use them in a RichTextBox they are.