Udostępnij za pośrednictwem


TextSelection.GetPropertyValue Method

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

Gets the value of the specified formatting property on the current selection.

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

Syntax

'Declaration
Public Function GetPropertyValue ( _
    formattingProperty As DependencyProperty _
) As Object
public Object GetPropertyValue(
    DependencyProperty formattingProperty
)

Parameters

Return Value

Type: System.Object
An object that indicates the value of the specified formatting property on the current selection.

Remarks

To return a property value, the entire TextSelection must extend over text with only one value for the formattingProperty. If the TextSelection extends over text with two or more different values for the formattingProperty, UnsetValue is returned.

Examples

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

Run this sample

    'Set Bold formatting to selected content
    Private Sub BtnBold_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If MyRTB IsNot Nothing Then
            If TypeOf MyRTB.Selection.GetPropertyValue(Run.FontWeightProperty) Is FontWeight _
                AndAlso (CType(MyRTB.Selection.GetPropertyValue(Run.FontWeightProperty), FontWeight) = FontWeights.Normal) Then
                MyRTB.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold)
            Else
                MyRTB.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal)
            End If
        End If
    End Sub

//Set Bold formatting to selected content
private void BtnBold_Click(object sender, RoutedEventArgs e)
{
    if (MyRTB != null)
    {
        if (MyRTB.Selection.GetPropertyValue(Run.FontWeightProperty) is FontWeight && ((FontWeight)MyRTB.Selection.GetPropertyValue(Run.FontWeightProperty)) == FontWeights.Normal)
            MyRTB.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold);
        else
            MyRTB.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.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.