How to put ellipsis (...) in RichTextBox - wpf

I want to put an ellipsis (..) in the rich text box.
In normal TextBlock, TextTrimming="WordEllipsis" has a property that limits the length to allow ellipsis representation, but rich text boxes do not. And it should only be implemented as a rich textbox now. Text blocks are not allowed.
I'd like to trim by two or three lines and add an ellipsis (..) option. Is there any good way?
I want to show you how I'm using RichTextBox, but the reputation is low.

You don't
The ellipsis concept is, as stated by grek40, something that only works when displaying text.
As an example, say the ellipsis is displayed, and the user tries to partially select some text in your RichTextBox, including the ellipsis, what would the selected result be? You can't tell.
Maybe
Since an ellipsis is usually a replacement for a Scrollbar, hiding text instead of allowing you to access it by scrolling, you might be able to fake it by using a WPF Style.
Create a Style that displays an icon/picture of an ellipsis (placement is up to you) whenever the Scrollbar visibility trigger is triggered. You would need to disable the Scrollbar once the ellipsis is visible.
This obviously requires more effort than simply setting a property, and it can easily become a User Experience nightmare if not carefully implemented, so be warned.
Note: Another comment (by Walt Ritscher) linked to a similar question, the solution there is similar to this one.
Alternative Maybe
Another faked ellipsis could be achieved by using two different RichTextBox controls.
The first RichTextBox would have set ReadOnly to true. Trim your text to a maximum allowed length, and append an ellipsis and display it inside this first RichTextBox.
When the user need to edit the text hide the first RichTextBox and display a second RichTextBox that contains your entire text.

Related

Does WPF Textbox respect Unicode BIDI controls like LRI/RLI?

I need to display English + Hebrew text mixed in a text box. I embed each language with the appropriate RLI/LRI/PDI controls but it seems that it doesn't make any difference. Depending on the content, things end up out of order, like if the BIDI controls are being ignored.
To check that my BIDI controls are placed correctly, I take the same text displayed in the textbox and put it in Clipboard. Pasting it to notepad or word displays the text in the expected order while on the textbox it isn't.
Can someone confirm if WPF textbox should respect these Unicode BIDI controls?
Am I missing any special setting on the textbox so it takes it into consideration?

Highlighting text in a textbox without using RichTextBox control

I have a WPF textbox (not a RichTextBox) and inside this textbox, I want to highlight search results (like in web browsers)
For example if I search for "abc", all occurences of "abc" should be highlighted (for example, with some red background or font)
I want to know if this is possible without using RichTextBox control, or not really?
It's possible but it's much more easy to use a RichTextBox so you may consider to use that instead, moreover you cannot change font size but only color (background and/or foreground) and effects.
First you have to derive your own class from TextBox because you'll override its render method. Now override the OnRender() method, here you'll use the DrawingContext.DrawText() method to draw the text (place everything inside a FormattedText object, primary you'll have to adjust its properties to make it similar to a standard TextBox).
Now what you have is a plain TextBox where in addition you draw your text. From this starting point you can choose to:
Completely override TextBox text drawing: set TextBox.Foreground property to Brushes.Transparent. User will interact with "real" text but he'll see the text your draw. Please note that to make this works you have to mimic exactly how text is drawn (if you change font size, for example, then they'll be unaligned) in the original TextBox.
Add the highlight feature you need keeping the base TextBox text drawing: calculate where the text you want to highlight is and then draw the proper background.
References
This (simplified!) algorithm comes from CodeBox2, it was originally designed to extend a TextBox with some simple editor-like features.
There is no built-in functionality for this. Also, the TextBox only supports a single fontstyle for the entire text.
If the text should be read-only, you could use a flow or fixed document and format the text in Run Elements.

Difference between Textbox and RichTextbox wpf?

Textbox and RichTextbox are look like same.But don't know the difference.Please tell me anyone, When i have to use TextBox and RichTextbox as well in wpf.
See this Microsoft overview of the differences between a TextBox and a RichTextBox.
From above Link:
Both RichTextBox and TextBox allow users to edit text, however, the two controls are used in different scenarios. A RichTextBox is a better choice when it is necessary for the user to edit formatted text, images, tables, or other rich content. For example, editing a document, article, or blog that requires formatting, images, etc is best accomplished using a RichTextBox. A TextBox requires less system resources then a RichTextBox and it is ideal when only plain text needs to be edited (i.e. usage in forms).
A RichTextBox mainly used if you want more control over styling the text color, type, font, alignment ect. So anything you can do in Microsoft Word, you can do with a RichTextBox.
It can be used to save or display .rtf files with ease.
A Textbox is basically used to display or get one line input. You can have a multi-line TextBox which is used mainly to display or get more than one one-liner and keeps you from having to
manage multiple TextBox's. Also keeps your UI a little more tidy.
So basically the main difference is in styling. If you just want something plain and simple, use TextBox. If you want something fancy, eg styles, colors use a RichTextBox.
Have a look at this:
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/a06ab05a-fbde-47ef-89c5-a7a57f32ffd3
A lot has been said about the differences in the controls' usage scenario.
An important technical difference is that TextBox supports DataBinding, RichTextBox does not, which makes it a bit nasty to use in an MVVM application. If you want a RichTextBox with DataBinding facilities, check out the extended WPF Toolkit on CodePlex.

Multiline WPF textblock: different alignment for lines

Is that possible to set different horizontal alignment for different lines of multiline textblock?
For example, I want to center header of my text, but main text I want to align by left side. I'd not want to use several textblocks for this issue.
Thanks.
I do not think you can get what you are looking for using the TextBlock control, this because the Inline elements that you can add in your TextBlock does not allow you to control the horizontal alignment (line by line).
Anyway I do not think that this is the best approach, in fact, as specified by MSDN:
TextBlock is not optimized for scenarios that need to display more than a few lines of content; for such scenarios, a FlowDocument coupled with an appropriate viewing control is a better choice than TextBlock, in terms of performance. After TextBlock, FlowDocumentScrollViewer is the next lightest-weight control for displaying flow content, and simply provides a scrolling content area with minimal UI. FlowDocumentPageViewer is optimized around "page-at-a-time" viewing mode for flow content. Finally, FlowDocumentReader supports the richest set functionality for viewing flow content, but is correspondingly heavier-weight.
So if you want to have more flexible control, and also better in terms of performance for what you ask it to do, you should use the FlowDocument.
I want to add a final remark. If you're looking for a dramatic improvement in performance, and you think that a single TextBlock is the right solution, I suggest to use Visual elements and the relative DrawText method instead. But if you're looking for an easy control to maintain in the future the choice is FlowDocument, as already said.

Passing a TextBox value to an IValueConverter used by another databound control in Silverlight 3?

I have a Silverlight 3 control where I am using an ItemsControl to display a list of items. I have implemented a "filter" or "search" textbox that allows the user to enter a search term in a textbox on the control that will limit the items displayed in the ItemsControl to ones that contain the string entered in the textbox. I have been able to implement this functionality, but I'd like to enhance it by changing the color of the text of the search term in the text that is displayed in the items control. For example, if the user types "foo" in the search textbox, I would like to filter the items in the ItemsControl (which I am already doing) to items that contain "foo", and change the background color of the substring "foo" where it occurs in the ItemsControl.
Ideally, I would implement an IValueConverter and pass the value in the search textbox as the parameter to the converter. The converter could then search through the value that is being converted and change the background color of all "foo" substrings (not sure how I'm going to do this yet, something with Inlines maybe?). It seems parameter values passed to value converters must be static resources. Is there any way I can pass a non-static value to the value converter to accomplish what I'm trying to do? Is there another way I should be going about this that does not use an IValueConverter?
Mind you I haven't worked this out completely yet, but I do something sort of similar in my app using Interaction.Behaviors. This is a little more flexible than a straight converter solution. In my solution, I use it to highlight cells and/or text in a grid based on various conditions (e.g. is this a critical item? Make it red).
Take a look at this blog entry, it explains the process fairly well, and should apply to more than just a Telerik RadGridView. (FYI, I think you'll need to add a reference to System.Windows.Interactivity)
Blog

Resources