WPF Markup Control - wpf

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.

Related

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.

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.

design time usercontrols - I can't see anything!

newbie question, please forgive...
I'm developing a Wpf UserControl that will eventually be bound to a business object. The usercontrol is little more than a series of laid out TextBlocks, and perhaps (later) an image or two.
As I'm laying out the usercontrol, I can put dummy text into all the TextBlocks so I can see what the usercontrol will look like, but as soon as I change the text property to contain the Binding information:
<TextBlock Margin="0,12.8,42,0" Name="lblLastName"
FontSize="8" Height="19"
VerticalAlignment="Top"
Text="{Binding Mode=OneWay, Path=LastName}"/>
Then I can no longer see the textbox, or any "placeholder" text. This makes it very difficult to adjust the location and sizes of all the controls on the UserControl. In WinFormas programming, you could set binding information independently of the Text property, so you could at least see the Placeholder text during design time development. It's going to be pretty hard to visually arrange a bunch of invisible TextBlocks! What's the standard solution for this?
You can change the Background of the TextBlock to something visible enough if it's the same color as the visuals behind it, and set HorizontalAlignment to "Stretch". This way it will fill the available width. That, or set the MinWidth to an explicit number. No text, but it will be visible.
I have successfully used design time mock objects to get a good preview of what everything will look like:
Viewing Design Time Data in Visual Studio 2008 Cider Designer in WPF and Silverlight Projects

Can I make WPF grid controls opaque at design time?

So I'm just starting out with WPF, and I'm really annoyed by the fact that if I lay two Grids on top of one another, the top Grid isn't opaque. It makes designing extremely annoying. Can this be turned off somehow?
I'm just building your standard Winforms STYLE application, but in WPF. I'm just trying to start bridging the gap here. In Winforms(and VB) you'd always have group boxes or something on your form, and then depending on some user context, one of those group boxes would be on top. Its how I've designed forms since time immemorial. One of two things must be true here:
A) This is not the recommended way to design Windows going forward with WPF, but I don't understand what you're supposed to do
B) There is some property to make the Grids opaque so I can build the Window in the style that I'm used to.
I'm fine with answers that solve either A or B. If I'm not doing things the right way because they've changed, then please enlighten me.
Update: So it turns out, I can make the grid opque by setting the background color, but now it seems like I'm locked into a White background as opposed to sticking with the system colors.
You could use SystemColors to make the control background colour match (rather than being white).
I don't understand why you want to put one grid on top of another though. In WPF you generally use a single grid to stack multiple visual elements within one region. Can you explain why you want to hide things in the background with foreground elements?
It sounds a little like you're implementing a tab control -- switching between pages of controls depending on focus. Have you experimented with the new TabControl?
I'm moving from WinForms to WPF development wherever possible and have found that in doing so it's taken some readjustment. WPF has a completely different way of laying things out and now that I'm more comfortable with it, I think it's superior. I'm guessing you just need to ride the learning curve a little longer.
Hope that helps.
EDIT: In response to your comment, I imagine you can have a tab control without tabs, though I haven't tried it myself (might be worthy of another question on SO). Tab controls are headered controls, meaning that they have a header item and a content item. In this case, the header is the tab button, the content is the page item. You can specify a ControlTemplate that details how these items should be displayed relative to one another.
Interestingly, many other types of common GUI element are also headered controls:
Menu items - The menu item text/icon is the header, and the optional submenu is the content
Tree view - Each node is the header, and optional children are within the content
Group box - The header is, well, the header and the content is, well, the content :)
Note that in the case of menu items and tree views, the type may recursively nest within itself. This is quite elegant and can give some wildly different presentation options over the same logical model with only changes to the control template.
For more information read about HeaderedContentControl and HeaderedItemsControl
You could use the following:
<Grid Background="{DynamicResource {x:Static SystemColors.WindowBrush}}">
<!-- content -->
</Grid>
This will respond to changes in the system colors on the fly (the DynamicResource does this).

Resources