TextRange.ApplyPropertyValue Method (DependencyProperty, Object)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

Namespace:   System.Windows.Documents
Assembly:  PresentationFramework (in PresentationFramework.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.

Exception Condition
ArgumentException

Occurs when formattingProperty does not specify a valid formatting property, or value specifies an invalid value for formattingProperty.

ArgumentNullException

Occurs when formattingProperty is null.

This method applies formatting by inserting the appropriate Inline elements, such as Bold and Italic, into the selection indicated by this TextRange.

The following example demonstrates the use of the ApplyPropertyValue method.

// This method accepts an input stream and a corresponding data format.  The method
// will attempt to load the input stream into a TextRange selection, apply Bold formatting
// to the selection, save the reformatted selection to an alternat stream, and return 
// the reformatted stream.  
Stream BoldFormatStream(Stream inputStream, string dataFormat)
{
    // A text container to read the stream into.
    FlowDocument workDoc = new FlowDocument();
    TextRange selection = new TextRange(workDoc.ContentStart, workDoc.ContentEnd);
    Stream outputStream = new MemoryStream();

    try
    {
        // Check for a valid data format, and then attempt to load the input stream
        // into the current selection.  Note that CanLoad ONLY checks whether dataFormat
        // is a currently supported data format for loading a TextRange.  It does not 
        // verify that the stream actually contains the specified format.  An exception 
        // may be raised when there is a mismatch between the specified data format and 
        // the data in the stream. 
        if (selection.CanLoad(dataFormat))
            selection.Load(inputStream, dataFormat);
    }
    catch (Exception e) { return outputStream; /* Load failure; return a null stream. */ }

    // Apply Bold formatting to the selection, if it is not empty.
    if (!selection.IsEmpty)
        selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);

    // Save the formatted selection to a stream, and return the stream.
    if (selection.CanSave(dataFormat))
        selection.Save(outputStream, dataFormat);

    return outputStream;
}

.NET Framework
Available since 3.0
Return to top
Show: