Method wpfBuilder.FormatText
Overload
Adds inlines (text, formatted text, hyperlinks, images, etc) to the last added System.Windows.Controls.TextBlock etc.
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 of the last added element. Or supported type but non-empty Content and Header (read Remarks). |
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 Inlines collection.
-
ContentControl (eg Label or Button) - creates new TextBlock with inlines and sets its Content property if it is
null
. If HeaderedContentControl (eg GroupBox) and its Header property isnull
, sets Header instead. -
Panel whose Parent is HeaderedContentControl (eg
b.StartGrid<GroupBox>(null).FormatText($"...")
) - uses the HeaderedContentControl like the above.
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='notepad.exe'>Notepad</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);