Method wpfBuilder.FormatText
Overload
Adds inlines (text, formatted text, hyperlinks, images, etc) to the last added element (eg System.Windows.Controls.TextBlock).
public wpfBuilder FormatText(wpfBuilder.InterpolatedString text)
Parameters
|
text (wpfBuilder.InterpolatedString)
Interpolated string (like These tags add inlines of these types:
Tags can have these attributes, like
WPF elements of these types can be inserted without tags:
XML special characters must be escaped:
The |
Returns
Exceptions
|
NotSupportedException
Unsupported type or content of the last added element. |
|
ArgumentException
Unknown |
|
InvalidOperationException
The same |
|
FormatException
Invalid color attribute. |
|
Exception
Exceptions of System.Xml.Linq.XElement.Parse. |
Remarks
The last added element can be of type:
- System.Windows.Controls.TextBlock - the function adds inlines to its
Inlinescollection. - System.Windows.Controls.ContentControl (eg System.Windows.Controls.Label or System.Windows.Controls.Button) or System.Windows.Controls.HeaderedContentControl (eg System.Windows.Controls.GroupBox) - creates new System.Windows.Controls.TextBlock with inlines and sets its
ContentorHeaderproperty. Or uses existingTextBlock. - wpfBuilder.Panel whose
Parentis System.Windows.Controls.HeaderedContentControl (egb.StartGrid<GroupBox>(null).FormatText($"...")) - uses the System.Windows.Controls.HeaderedContentControl like the above. RichTextBox,FlowDocumentScrollViewerorFlowDocumentReaderwith non-nullDocument- adds single System.Windows.Documents.Paragraph with inlines. Or uses existing singleParagraph.
For elements other than the last added use wpfBuilder.formatTextOf or wpfBuilder.formattedText.
To load images can be used ImageUtil.LoadWpfImageElement and ImageUtil.LoadWpfImage.
Examples
b.R.Add<TextBlock>().FormatText($"""
Text <b>bold</b> <i>italic <u>underline</u>.</i>
<s c='GreenYellow' b='Black' FontFamily='Consolas' FontSize='20'>attributes</s>
<s {new Span() { Foreground = Brushes.Red, Background = new LinearGradientBrush(Colors.GreenYellow, Colors.Transparent, 90) }}>Span object, <b>bold</b></s>
<a href='https://www.example.com'>example.com</a> <b><a href='mspaint.exe'>mspaint</a></b>
<a {() => { print.it("click"); }}>click</a> <a {(Hyperlink h) => { print.it("click once"); h.IsEnabled = false; }}>click once</a>
<a {() => { print.it("click"); }} ToolTip='image hyperlink'>{ImageUtil.LoadWpfImageElement("*Entypo.HelpWithCircle #008EEE @14")}</a>
{new Run("Run object") { Foreground = Brushes.Blue, Background = Brushes.Goldenrod, FontSize = 20 }}
Image {ImageUtil.LoadWpfImageElement("*PixelartIcons.Notes #0060F0")}<!-- or ImageUtil.LoadWpfImage(@"C:\Test\image.png") -->
Controls {new TextBox() { MinWidth = 100, Height = 20, Margin = new(3) }} {new CheckBox() { Content = "Check" }}
""");
Build interpolated string at run time.
wpfBuilder.InterpolatedString s = new();
s.AppendLiteral("Text <b>bold</b> <a ");
s.AppendFormatted(() => { print.it("click"); });
s.AppendLiteral(">link</a>.");
b.R.Add<TextBlock>().FormatText(s);