I want a multiline textbox which shows apostrophes before and after each line.
So that the textbox looks like:
" Hello this "
" is a funny "
" string test "
Or for example:
// This is
// a muliline
// comment.
Edit: These special characters must be readonly and if the user copies text from the textbox these characters should not be included.
This is very easy using two textboxes laid directly over one another. The rear one is a normal textbox with extra padding and transparent text. The front one has your extra characters but has its borders hidden and is
IsHitTestVisible=FalseandFocusable=Falseso it doesn’t interact with the user. The user interacts exclusively with the rear textbox but the front textbox is the one that displays the text. A binding with a value converter keeps the front textbox displaying exactly what the rear textbox displays, plus the extra characters.This is how it would look:
ExtraCharacterConverter is a simple
IValueConverterclass that implements theConvertmethod by taking the given string, appending quotes or // or whatever to it, and returning the result.Note that I hard-coded a left and right margin of 10 units on the rear textbox, which assumes a particular width for the quote characters. This should be exactly the width of the added characters to make the text line up correctly. You want to get this right, or your caret and text selection positioning will be wrong. Also note that the correct value will change as you vary your font size and your choice of extra characters.
An easy alternative to hard-coding the margin would be to set it to a multi-binding on
FontSize, FontFamily, FontWeight, etc, then use aIMultiValueConverterto compute the proper margin given this these values.Note: This solution is slighly unsatisfactory when it comes to the color scheme for text selection. This can be fixed, but it requires a more complex solution: The rear text box is the same but its text is not invisible. The front text box is replaced with a RichTextBox (or TextBlock) whose content is computed dynamically to be the text with extra characters, but the regular text transparent. Because it is a RichTextBox the extra characters can be visible while the others are transparent.