Silverlight Text - Microsoft Silverlight

What is Silverlight Text?

In this section, we will look at what Silverlight proposals to display text. The text block is used for all text rendering and Silverlight. Other important features are −

  • It can be used to simple plain text or you can apply a mixture of formatting styles.
  • Silverlight supports a standard set of built in fonts.
  • You can also download custom fonts when your applications visual style need something less ordinary.

TextBlock

To show text we use Silverlight textbook element, which is a trivial control for showing small amounts of read-only text. In fact, we have previously seen this quite a lot as its simple usage does not really need much clarification. You just set the text assets and it displays that text for you.

The hierarchical inheritance of TextBlock class is as follows,

textblock
Given below are the commonly used properties of TextBlock class.

Sr. No.

Property & Description

1

ContentEnd

Gets a TextPointer object for the end of text content in the TextBlock.

2

ContentStart

Gets a TextPointer object for the start of text content in the TextBlock.

3

IsTextSelectionEnabled

Gets or sets a value that indicates whether text selection is enabled in the TextBlock, either through user action or calling selection-related API.

4

IsTextSelectionEnabledProperty

Identifies the IsTextSelectionEnabled dependency property.

5

LineHeight

Gets or sets the height of each line of content.

6

MaxLines

Gets or sets the maximum lines of text shown in the TextBlock.

7

SelectedText

Gets a text range of selected text.

8

SelectionEnd

Gets the end position of the text selected in the TextBlock.

9

SelectionHighlightColor

Gets or sets the brush used to highlight the selected text.

10

SelectionStart

Gets the starting position of the text selected in the TextBlock.

11

Text

Gets or sets the text contents of a TextBlock.

12

TextAlignment

Gets or sets a value that indicates the horizontal alignment of text content.

13

TextTrimming

Gets or sets the text trimming behavior to employ when content overflows the content area.

14

TextWrapping

Gets or sets how the TextBlock wraps text.

Given below are commonly used events of TextBlock class.

Sr. No.

Event & Description

1

ContextMenuOpening

Occurs when the system processes an interaction that displays a context menu.

2

SelectionChanged

Occurs when the text selection has changed.

Given below are the commonly used methods in TextBlock class.

Sr. No.

Method & Description

1

Focus

Focuses the TextBlock, as if it were a conventionally focusable control.

2

Select

Selects a range of text in the TextBlock.

3

SelectAll

Selects the entire contents in the TextBlock.

Run

Occasionally you want fine-grained switch over arranging and setting one style for an entire text block. It is occasionally useful to format separate words or even letters, and if you want this then instead of using the Text property, you put the text inside the TextBlock as content. If you are using a code, this corresponds to adding items to the TextBlock inline property.

Using this method, you can add a series of run basics. Each Run supports the same font family, front weight, foreground and so on properties for controlling the text style. Although Run is a separate element this does not disrupt the flow.

Let us have a look at a simple example, which contains multiple Run element inside TextBlock. Given below is the XAML code.

When the above code is compiled and executed, you will see the following output.

run_inside_textblock

As you can see, this text block is arranged with different formatting styles by using the Run element.
By the way, you do not need to wrap every single bit of text in a run. You can leave most of the content of a text block as plain text and just apply run to the parts that need different formatting as shown below.

LineBreak

Silverlight usually ignores line breaks in the XAML. It assumes that most white spaces are there to make them easier to read because you actually want that space to appear.

Let us have a look at this XAML code, which has three separate lines of text in it.

When the above code is compiled and executed, you will see the following output.

linebreak

As you can see that it has ignored the line breaks and executed all the text together.

  • If you enable text wrapping, it will put line breaks in where it needs to be to make the text fit but it will ignore the line breaks in your example.
  • If you just want to add explicit line breaks, you need to add a line break tag inside your text block. The text follows it will start on a new line.

Let us have a look at the same example again by adding the LineBreak tag.

When the above code is executed, you will see the that it now looks like as specified in XAML.

add_linebreak_tag

Built-in Fonts

Silverlight has a secure set of built-in font families. The fonts really have different family names for historical reasons. The default family is strictly different on Mac OS and windows such as on Mac OS it is Lucida Grande, while on Windows it is the almost identical but named Lucida Sans Unicode.

Some of the most commonly used fonts are given below.

Fonts

Arial

Arial Black

Comic Sans MS

Courier New

Georgia

Lucida Grande (Mac) or Lucida Sans Unicode (Windows)

Times New Roman

Trebuchet MS

Verdana


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Microsoft Silverlight Topics