Freigeben über


TextBox.FontSource Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the font source that is applied to the TextBox for rendering content.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property FontSource As FontSource
public FontSource FontSource { get; set; }

Property Value

Type: System.Windows.Documents.FontSource
The font source used to render content in the text box. The default is nulla null reference (Nothing in Visual Basic).

Exceptions

Exception Condition
ArgumentOutOfRangeException

The value set is not a valid source.

Remarks

If the FontSource property is set to nulla null reference (Nothing in Visual Basic), any assigned custom font is cleared and the TextBox renders with the default font.

Examples

The following code example shows how you can set the FontSource property.

Private Sub AddFont()

    'Set the font to Times New Roman using FontSource property.
    Dim MyTextBox As TextBox = New TextBox
    MyTextBox.Text = "hello world"
    Dim MyUri As Uri = New Uri("times.ttf", UriKind.Relative)
    Dim MySRI As StreamResourceInfo = Application.GetResourceStream(MyUri)
    MyTextBox.FontSource = New FontSource(MySRI.Stream)
    MyTextBox.FontFamily = New FontFamily("Times New Roman")

    'Add the textbox to the stackpanel
    MyStackPanel.Children.Add(MyTextBox)
End Sub
private void AddFont()
{
    //Set the font to Times New Roman using FontSource property.
    TextBox MyTextBox = new TextBox();
    MyTextBox.Text = "hello world";
    Uri MyUri = new Uri("times.ttf", UriKind.Relative);
    StreamResourceInfo MySRI = Application.GetResourceStream(MyUri);
    MyTextBox.FontSource = new FontSource(MySRI.Stream);
    MyTextBox.FontFamily = new FontFamily("Times New Roman");

    //Add the textbox to the stackpanel
    MyStackPanel.Children.Add(MyTextBox);

 }

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.