Windows Dev Center

TextSelection.ApplyPropertyValue Method

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

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

public void ApplyPropertyValue(
	DependencyProperty formattingProperty,
	Object value
)

Parameters

formattingProperty
Type: System.Windows.DependencyProperty
A formatting property to apply.
value
Type: System.Object
The value for the formatting property.

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.

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.


//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);
    }


}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

Show:
© 2015 Microsoft