Articles in this section
Category / Section

How to scroll SfRichTextBoxAdv programmatically in UWP application?

2 mins read

The SfRichTextBoxAdv control uses the scrollbars defined in its template. Please find the template of SfRichTextBoxAdv from below UG link :

https://help.syncfusion.com/uwp/sfrichtextboxadv/styles-and-templates

You can implement an extension class to retrieve the scrollbars of SfRichTextBoxAdv using GetTemplateChild method. In extension class, you can add your own APIs to perform the scrolling.

The following codes shows how to get the scrollbars of SfRichTextBoxAdv and implement APIs for scrolling in extension class.

C#:

/// <summary>
/// Represents the extension class for SfRichTextBoxAdv.
/// </summary>
public class SfRichTextBoxAdvExtension : SfRichTextBoxAdv
{
    #region Fields
    ScrollBar verticalScrollBar;
    ScrollBar horizontalScrollBar;
    #endregion
 
    #region Properties
    /// <summary>
    /// Gets or sets the verical scrollbar value.
    /// </summary>
    public double VScrollValue
    {
        get
        {
            if (verticalScrollBar != null)
                return verticalScrollBar.Value;
            return 0;
        }
        set
        {
            if (verticalScrollBar != null)
                verticalScrollBar.Value = value;
        }
    }
    /// <summary>
    /// Gets or sets the horizontal scrollbar value.
    /// </summary>
    public double HScrollValue
    {
        get
        {
            if (horizontalScrollBar != null)
                return horizontalScrollBar.Value;
            return 0;
        }
        set
        {
            if (horizontalScrollBar != null)
                horizontalScrollBar.Value = value;
        }
    }
    /// <summary>
    /// Gets the vertical scroll bar maximum value.
    /// </summary>
    public double VScrollMaxValue
    {
        get
        {
            if (verticalScrollBar != null)
                return verticalScrollBar.Maximum;
            return 0;
        }
    }
    /// <summary>
    /// Gets the horizontal scroll bar maximum value.
    /// </summary>
    public double HScrollMaxvalue
    {
        get
        {
            if (horizontalScrollBar != null)
                return horizontalScrollBar.Maximum;
            return 0;
        }
    }
    #endregion
 
    #region Constructor
    /// <summary>
    /// Initializes the instance of SfRichTextBoxAdvExtension class.
    /// </summary>
    public SfRichTextBoxAdvExtension()
    {
        // Wires the Loaded event.
        this.Loaded += SfRichTextBoxAdvExtension_Loaded;
    }
 
    #region Events
    /// <summary>
    /// Called when SfRichTextBoxAdv is loaded.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void SfRichTextBoxAdvExtension_Loaded(object sender, RoutedEventArgs e)
    {
        verticalScrollBar = GetTemplateChild("VerticalScrollBar") as ScrollBar;
        horizontalScrollBar = GetTemplateChild("HorizontalScrollBar") as ScrollBar;
    }
    #endregion
 
    #region Implementation
    /// <summary>
    /// Do the vertical scroll.
    /// </summary>
    /// <param name="deltaValue"></param>
    public void DoVerticalScroll(double deltaValue)
    {
        if (deltaValue != 0 && verticalScrollBar != null)
        {
            double previousValue = VScrollValue;
            if (previousValue + deltaValue <= VScrollMaxValue)
                VScrollValue = previousValue + deltaValue;
            else
                VScrollValue = VScrollMaxValue;
        }
    }
    /// <summary>
    /// Do the horizontal scroll
    /// </summary>
    /// <param name="deltaValue"></param>
    public void DoHorizontalScroll(double deltaValue)
    {
        if (deltaValue != 0 && horizontalScrollBar != null)
        {
            double previousValue = HScrollValue;
            if (previousValue + deltaValue <= HScrollMaxvalue)
                HScrollValue = previousValue + deltaValue;
            else
                HScrollValue = HScrollMaxvalue;
        }
    }
    /// <summary>
    /// Disposes the instance.
    /// </summary>
    public void Dispose()
    {
        verticalScrollBar = null;
        horizontalScrollBar = null;
        this.Loaded -= SfRichTextBoxAdvExtension_Loaded;
        base.Dispose();
    }
    #endregion
}

 

XAML:

<local:SfRichTextBoxAdvExtension x:Name="richTextBoxAdv" ManipulationMode="All" />

 

The following code demonstrates scrolling SfRichTextBoxAdv programmatically.

 

// Performs vertical scroll (down to 100 pixels.)
richTextBoxAdv.DoVerticalScroll(100);
 
// Performs vertical scroll (up to 100 pixels.)
richTextBoxAdv.DoVerticalScroll(-100);

// Performs Horizontal scroll (right to 100 pixels.)
richTextBoxAdv.DoHorizontalScroll(100);
 
// Performs Horizontal scroll (left to 100 pixels.)
richTextBoxAdv.DoHorizontalScroll(-100);
 
// Vertical scroll bar scrolled to specified pixel value.
richTextBoxAdv.VScrollValue = 150;
 
// Horizontal scroll bar scrolled to specified pixel value.
richTextBoxAdv.HScrollValue = 120;

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied