Category / Section
How to scroll SfRichTextBoxAdv programmatically in WPF application?
1 min read
The SfRichTextBoxAdv control uses the scrollbars defined in its template. Please find the template of SfRichTextBoxAdv from the following 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 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 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 of 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 in SfRichTextBoxAdv programmatically.
// Performs vertical scroll (down by 100 pixels.) richTextBoxAdv.DoVerticalScroll(100); // Performs vertical scroll (up by 100 pixels.) richTextBoxAdv.DoVerticalScroll(-100); // Performs horizontal scroll (right by 100 pixels.) richTextBoxAdv.DoHorizontalScroll(100); // Performs horizontal scroll (left by 100 pixels.) richTextBoxAdv.DoHorizontalScroll(-100); // Vertical scrollbar scrolled by specified pixel value. richTextBoxAdv.VScrollValue = 150; // Horizontal scrollbar scrolled by specified pixel value. richTextBoxAdv.HScrollValue = 120;