public static FormattingTag GetTag(TextPointer start, int startIndex, int length, DependencyProperty formattingProperty, object value)
    {
        while (start.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.Text)
        {
            start = start.GetNextContextPosition(LogicalDirection.Forward);
        }

        TextPointer   contentStart = start.GetPositionAtOffset(startIndex);
        TextPointer   contentEnd   = contentStart.GetPositionAtOffset(length);
        FormattingTag tag          = new FormattingTag(startIndex, length);

        tag.StartPosition      = contentStart;
        tag.EndPosition        = contentEnd;
        tag.FormattingProperty = formattingProperty;
        tag.Value = value;
        return(tag);
    }
Exemple #2
0
    public void ApplyPropertyValue(int startIndex, int length, DependencyProperty formattingProperty, object value)
    {
        TextRange documentRange = new TextRange(this.Document.ContentStart, this.Document.ContentEnd);

        documentRange.ClearAllProperties();
        string documentText = documentRange.Text;

        if (startIndex < 0 || (startIndex + length) > documentText.Length)
        {
            return;
        }

        this.CaretPosition = this.Document.ContentStart;
        this.formattingTags.Add(FormattingTag.GetTag(this.Document.ContentStart, startIndex, length, formattingProperty, value));

        foreach (var formattingTag in formattingTags)
        {
            formattingTag.ApplyFormatting();
        }
    }