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 the 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 the GetTemplateChild method. In the extension class, you
can add your own APIs to perform scrolling.
The following code shows how to get the scrollbars of SfRichTextBoxAdv and
implement APIs for scrolling in the 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 vertical 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 scrollbar maximum value. /// </summary> public double VScrollMaxValue { get { if (verticalScrollBar != null) return verticalScrollBar.Maximum; return 0; } } /// <summary> /// Gets the horizontal scrollbar maximum value. /// </summary> public double HScrollMaxValue { get { if (horizontalScrollBar != null) return horizontalScrollBar.Maximum; return 0; } } #endregion #region Constructor /// <summary> /// Initializes the instance of the 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> /// Performs the vertical scrolling. /// </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> /// Performs the horizontal scrolling. /// </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 scrollbar scrolled to the specified pixel value. richTextBoxAdv.VScrollValue = 150; // Horizontal scrollbar scrolled to the specified pixel value. richTextBoxAdv.HScrollValue = 120;