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.
Related
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);
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 making WPF application.I have a data grid and all columns have set their width to "Auto".When i start scrolling through the table some of the columns start expanding.My question is: Can I set the column width to fit the longest data in column at the beginning (without expanding columns when i scroll)?
the column width for wpf expanding when the shown data need more space when it set to Auto then this work will be done automatically.
Well this is due to the virtualization of the DataGrid. Only the items, which are visible are rendered and the controls are reused in case of scrolling (if you are activating it). And so the width will not be correct, since the longest element is not rendered yet. A list control performs very well, although you might bind many items to it.
I think you have 2 options
Turning off the virtualization, which might be a proper solution, if you don't have many items to show. I've to admit, I didn't try it, so no warranty. You can turn it off via <DataGrid VirtualizingStackPanel.IsVirtualizing="False"/>. For more information on the VirtualizationStackPanel pls have a look here.
Another solution may be TextTrimming. TextBlocks can show Ellipses if the text is too long. Therefor you will have to assign a custom datatemplate to the column put the following as content e.g. <TextBlock Text="{Binding}" TextTrimming="WordEllipsis"/>. Please note, that you will also have to provide a customer CellEditingTemplate, if the user shall be able to edit the values. For more information about TextTrimming please have a look here. To get an idea how the whole DataTemplate thing regarding DataGrids will work, you can have a look here.
Please refer this control
http://www.charlespetzold.com/blog/2009/10/Using-Text-Outlines-in-Silverlight.html
The formattedtext control is a shape which helps to generate the shape of the text with proper geometry. I would like to make this control act like a text box with cursors and features like typing in from keyboard.
Right now I use an invisible text box with a formattedtext control to act like that. But the cursor position always creates a problem when the size of the text is not equal to the size of the rendered text as shape.
Can anyone please show the way to achieve this.
Well, I built a syntax highlighting textbox using the method you describes.
Actually, at first I wanted to rebuilt everything too, but I thought : I have to build the caret fonctionnalities, the selection brush, manage a lot of different events, like selection with mouse or keyboard, deletion, Copy/Cut/Paste, etc etc...
That's a LOT of work, and windows users are used to select text in textboxes for instance, so this complex implementation cannot be left unimplemented. We must follow some Microsoft guidance on how a textbox must feel.
Actually, I think that building a new textbox from scratch is not the way to go. I suggest you to continue on your current method. If you have different fonts in the same textbox, use a RichTextBox, and handle the font changes in the textbox as well as in the formatted text.
Also, a good think to implement is to only draw the visible text with the formattedtext (but only if the user can write several hundreds of text lines).
I have a list that the user can filter in several ways. two of which lend themselves to combo boxes and two that need to accept user input. For example, one textbox allows the user to type in any part of a list item's description, and the dialog will only present items whose description contains the text entered.
It is a dialog 'picker' type of window, so space is at a premium. I'd like for the text boxes not to require a traditional label. Instead, when the dialog is first invoked, the label (ie, "Description") is grayed out, centered, and in italics. Maybe a tool tip to further make it obvious to the user what it's for. When the user starts to type, the faux label disappears and the entered text is normal left aligned text.
Does wpf / silverlight have any native support for doing something like this? I guess it could be a combination of styles and eventing. I'd rather not invent any wheels that might be out there (I got the idea specifically from looking at Tortoise' "Show Log" window, but I've seen it before).
Does anyone have any sample code they can share to do this? Or a an alternative idea that also saves space and simplifies the layout?
Cheers,
Berryl
Kevin Moore's InfoTextBox, which is part of his Bag-O-Tricks is the kind of thing I was looking for, almost exactly. This is also the 'watermark' (great name - I would have found this sooner if I had known that) text box from another post.