Partager via


TextSelection.ApplyPropertyValue Method

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

Applies the specified formatting property and value to the current selection.

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

Syntax

'Declaration
Public Sub ApplyPropertyValue ( _
    formattingProperty As DependencyProperty, _
    value As Object _
)
public void ApplyPropertyValue(
    DependencyProperty formattingProperty,
    Object value
)

Parameters

  • value
    Type: System.Object
    The value for the formatting property.

Remarks

This method applies specified formatting by inserting the appropriate Inline elements, such as Bold and Italic, into this TextSelection. Sometimes the ApplyPropertyValue method may modify or convert TextElement objects when it applies the specified formatting values. For example, a Span element may be converted to a Run element or similar property values may be combined to normalize them.

Examples

The following code uses the ApplyPropertyValue method to apply Italic formatting to selected content in a RichTextBox. This code example is a part of a larger example used in the TextSelection class.

Run this sample

    'Set Italic formatting to selected content
    Private Sub BtnItalic_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If MyRTB IsNot Nothing Then
            If TypeOf MyRTB.Selection.GetPropertyValue(Run.FontStyleProperty) Is FontStyle _
                AndAlso (CType(MyRTB.Selection.GetPropertyValue(Run.FontStyleProperty), FontStyle) = FontStyles.Normal) Then
                MyRTB.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Italic)
            Else
                MyRTB.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Normal)
            End If
        End If
    End Sub

//Set Italic formatting to selected content
private void BtnItalic_Click(object sender, RoutedEventArgs e)
{
    if (MyRTB != null)
    {
        if (MyRTB.Selection.GetPropertyValue(Run.FontStyleProperty) is FontStyle && ((FontStyle)MyRTB.Selection.GetPropertyValue(Run.FontStyleProperty)) == FontStyles.Normal)
            MyRTB.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Italic);
        else
            MyRTB.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Normal);
    }


}

Version Information

Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

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