Difference between Textbox and RichTextbox wpf? - 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.

Related

WPF Markup Control

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.

Problems writing too much custom controls in WPF

I am working on new WPF/MVVM project, where I see all most all controls are written for different needs, right from textbox to treeview. All are rewritten for simple need, for example, grid,stackpanel control are rewritten to add space between each item and textbox is rewritten to include label for it so that it has both label and text input are on itself.
My question : Is there any serious issue we would encounter because of this customization?
Already, I am seeing issues with aligning all controls, will i would see any more issues because of this?.
You should never create a custom or user control to add margins, a label to a TextBox, or a new ItemTemplate to a ListBox.
UserControls are for grouping frequently used combinations of controls into one reuseable control. An example may be a custom List-of-Values control that opens a dialog. This would be fine to implement as a UserControl.
Custom controls are good when a native control does not suit your needs. Say that you want to reimplement a DateTimePicker from scratch because the native one doesn't include milliseconds.
There are no serious issues as such, but you may find yourself maintaining all these controls for the next many years without there being a need to do so.
Settings Margins should be done in the View where you are using it, or on a Style in a ResourceDictionary.
This is of course only my opinion (and that of many others, I except), but if you find that the majority of your controls are 'customized' this way, you are doing it wrong.
Style and Templates rather than UserControls and custom controls.
The main issue is that you lose the ability to alter margins only in a single view. If you change your custom controls' inner paddings and margins, you will be changing all the views in your solution. If you use a style, you can always override it by defining a new style in the view, or by setting the property directly.

Displaying dynamic text with hyperlinks in Silverlight

I have an application that should work a bit like a message board: text gets parsed, hyperlinks are made "live" and displayed. Only it's in Silverlight. Because of that, I can't use TextBlock like they do here WPF - Making hyperlinks clickable, somehow it's different in Silverlight and it's not possible to do a Hyperlink with a Run inside it.
I've tried the RichTextBlock, as it says here Wrapping Text and Hyperlinks in Silverlight, but couldn't for the life of me figure out how to create the paragraphs and hyperlinks from code, since I need to populate it dynamically.
Then I thought I'd settle for a less elegant solution involving TextBlocks and HyperlinkButtons in a WrapPanel. I added a namespace reference, like it says here WrapPanel in Silverlight 4 toolkit, but when I reference it in XAML, there's no WrapPanel in it, only DataField, DataForm and VisualStateManager.
This Text areas and hyperlinks? doesn't work either, the Silverlight RichTextBox is different and LinkLabel is missing.
Maybe there's another solution that I don't know? I'm keeping an open mind. Any help appreciated.

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.

Custom Text Wrapping in WPF

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.

Resources