Is there a way of wrapping text in a non-rectangular container in WPF?
Here is how it is done in photoshop
Unfortunately there isn't a straightforward way without making a complete implementation of a TextFormatter. MSDN article on the basics of an Advanced TextFormatter:
The text layout and UI controls in WPF provide formatting properties that allow you to easily include formatted text in your application. These controls expose a number of properties to handle the presentation of text, which includes its typeface, size, and color. Under ordinary circumstances, these controls can handle the majority of text presentation in your application. However, some advanced scenarios require the control of text storage as well as text presentation. WPF provides an extensible text formatting engine for this purpose.
Have you looked at the UIElement.Clip property?
For non-rectangular text wrapping, you could try setting a TextBlock.Clip property to a non-rectangular Geometry object. I haven't tried this; either it will not draw text outside the clip region or it will wrap text to fit within the clip.
Charles Petzold mentions this technique.
Related
I have a string of text that I need to display. I currently display it in a textbox.
My requirements have changed and now I need to display parts of this string in Red.
I have looked and all I can see for displaying text in several colors is a WebBrowser or RichTextbox. Both of these are more complex then I was hoping to use.
I can format the text string in any way (using any kind of markup).
Is there some kind of simple markup control out there for wpf? (Note: This will be going on a datagrid that can have many hundreds of rows, so it cannot be a memory/processing intensive control.)
You can use a TextBlock if it doesn't need to be edited.
From above link:
TextBlock is designed to be lightweight, and is geared specifically at integrating small portions of flow content into a user interface (UI). TextBlock is optimized for single-line display, and provides good performance for displaying up to a few lines of content.
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.
Modified example from link showing Red Text:
<Grid>
<TextBlock TextWrapping="Wrap" >
<Bold>TextBlock</Bold> is designed to be <Italic>lightweight</Italic>
and is geared <Run Foreground="Red">specifically</Run> at integrating
<Italic>small</Italic> portions of flow content into a UI.
</TextBlock>
</Grid>
AvalonEdit should do what you want. There is a Nuget package for it. See here also:
http://www.codeproject.com/Articles/42490/Using-AvalonEdit-WPF-Text-Editor
Also, if the text is not being edited, I think you should probably use something like a ListBox with custom item templates -- especially if the entire line is to be highlighted.
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.
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.
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.
Is there any way to set style for my UserControl to look like polygon, for example triangle or sth more sophisticated? I was reading that in WPF there is almost no limits when it comes to modifing graphical interface
WPF has a "lookless" approach to controls and UI. This allows the developer to strongly separate the business logic from the UI logic and also helps to lend itself to MVVM. What this means for you is that you can use a control template or style to redefine the look of your control. To name a couple of options, you can use an image, or you could use a Path element. choose whatever works best for you. Path has the benefit of being a "vector" drawing that will resize automatically-like. Here is a nice article on WPF and "lookless" controls.