Format painter. How to change selected text style?

6 posts, 0 answers
  1. Tom
    Tom avatar
    6 posts
    Member since:
    Feb 2017

    Posted 14 Nov Link to this post

    Hi,

    I want to change selected text style. I don't have problem with change one word or line but i don't know how to select exact text and change style.

    Below is code how i change style selected line.

    private void Editor_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {      

                    var normalStyle = editor.Document.StyleRepository[HeadingName];
                    var position = new DocumentPosition(editor.Document.CaretPosition);
                    position.MoveToCurrentLineStart();
                    editor.Document.Selection.AddSelectionStart(position);
                    position.MoveToCurrentLineEnd();
                    editor.Document.Selection.AddSelectionEnd(position);

                    editor.ChangeSpanStyle(normalStyle);

           }

    I don't find any solution to solve this problem.

    Any suggestion?

  2. Rick
    Rick avatar
    3 posts
    Member since:
    Nov 2013

    Posted 15 Nov in reply to Tom Link to this post

    Hi Tom,

    I am also looking to implement format painter. Do you have any suggestions? Is it possible for you to share the code for getting the style when clicking on "Format painter" button?

    Thanks

     

  3. Tom
    Tom avatar
    6 posts
    Member since:
    Feb 2017

    Posted 15 Nov in reply to Rick Link to this post

     

    code behind:

    Format painter button is RadRibbonToggleButton

     

    public Span SpanCopy { get; private set; }
    public string HeadingName { get; private set; }

    private void FormatPainterButton_OnChecked(object sender, RoutedEventArgs e)
            {
                if (SpanCopy == null)
                {
                    SpanCopy = editor.Document.CaretPosition.GetCurrentSpanBox().AssociatedSpan;
                    var paragraph = SpanCopy.Parent as Paragraph;
                    HeadingName = paragraph.Style.Name;
                }
            }

    private void FormatPainterButton_OnUnchecked(object sender, RoutedEventArgs e)
            {
                SpanCopy = null;
                HeadingName = null;
            }

    private void Editor_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                if (SpanCopy != null)
                {
                    editor.Document.Selection.Clear();

                    var normalStyle = editor.Document.StyleRepository[HeadingName];
                    var position = new DocumentPosition(editor.Document.CaretPosition);

                    position.MoveToCurrentLineStart();
                    editor.Document.Selection.AddSelectionStart(position);
                    position.MoveToCurrentLineEnd();
                    editor.Document.Selection.AddSelectionEnd(position);


                    editor.ChangeSpanStyle(normalStyle);
                    editor.ChangeTextForeColor(SpanCopy.ForeColor);
                    editor.ChangeFontSize(SpanCopy.FontSize);
                    editor.ChangeFontStyle(SpanCopy.FontStyle);
                    editor.ChangeFontFamily(SpanCopy.FontFamily);

                    editor.ChangeFontWeight(SpanCopy.FontWeight);

                    formatPainterButton.IsChecked = false;
                }
                SpanCopy = null;
                HeadingName = null;
            }

    xaml:

          <telerik:DocumentRuler Grid.Row="1">
                <telerik:RadRichTextBox Name="editor"                                   
                                        LayoutMode="Paged" MouseLeftButtonUp="Editor_OnMouseLeftButtonUp" />
            </telerik:DocumentRuler>

     

     

    position.MoveToCurrentLineStart(); and position.MoveToCurrentLineEnd(); - You can change position to current word, line, paragraph.. but i don't know how to change select exact text


     

  4. Rick
    Rick avatar
    3 posts
    Member since:
    Nov 2013

    Posted 15 Nov in reply to Tom Link to this post

    Thanks Tom. Our approaches are similar. We will let you know after we have made some progress in implementing the format painter (just like word).
  5. Anna
    Admin
    Anna avatar
    115 posts

    Posted 16 Nov Link to this post

    Hello Tom,

    Thank you for your question and for providing your code.

    I am happy to say that the feature is included in our backlog and we are hoping to implement it in the near future. You can find a link to the item in our portal here.

    As to your question, shouldn't the format painter take the already existing selection and set the format properties to it? In other words you wouldn't have to change the selection, unless it is empty and in that case you would only need to select the current word. I would change the implementation to something along these lines:

    private void Editor_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (SpanCopy != null)
        {
            var normalStyle = editor.Document.StyleRepository[HeadingName];
            var position = new DocumentPosition(editor.Document.CaretPosition);
     
            if (editor.Document.Selection.IsEmpty)
            {
                this.PaintFormatOnEmptySelection(position, normalStyle);
            }
            else
            {
                this.PaintFormatOnCurrentSelection(normalStyle);
            }
     
            formatPainterButton.IsChecked = false;
        }
     
        SpanCopy = null;
        HeadingName = null;
    }
     
    private void PaintFormatOnEmptySelection(DocumentPosition position, StyleDefinition style)
    {
        position.MoveToCurrentWordStart();
        editor.Document.Selection.AddSelectionStart(position);
        position.MoveToCurrentWordEnd();
        editor.Document.Selection.AddSelectionEnd(position);
     
        this.PaintProperties(style);
     
        editor.Document.Selection.Clear();
    }
     
    private void PaintFormatOnCurrentSelection(StyleDefinition style)
    {
        this.PaintProperties(style);
    }
     
    private void PaintProperties(StyleDefinition style)
    {
        editor.ChangeSpanStyle(style);
        editor.ChangeForeColor(SpanCopy.ForeColor);
        editor.ChangeFontSize(SpanCopy.FontSize);
        editor.ChangeFontStyle(SpanCopy.FontStyle);
        editor.ChangeFontFamily(SpanCopy.FontFamily);
     
        editor.ChangeFontWeight(SpanCopy.FontWeight);
    }

    Please let me know if I understood your scenario correctly.

    Regards,
    Anna
    Progress Telerik
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
  6. Tom
    Tom avatar
    6 posts
    Member since:
    Feb 2017

    Posted 17 Nov in reply to Anna Link to this post

    Hi Anna,

    Thanks for improve my code. This is exactly what i needed.

    Regards
Back to Top