Search code examples
wpftextboxcustom-event

WPF- Is it possible to add an OnVerticalOffsetChanged event to a custom textbox?


Is there any way for me to do this?


Solution

  • You can tell when the VerticalOffset changes by adding a handler to the ScrollViewer.ScrollChanged event to your TextBox. Something like this:

    <TextBox AcceptsReturn="True" ScrollViewer.ScrollChanged="TextBox_ScrollChanged" />
    

    The TextBox uses a ScrollViewer internally, so it's ScrollChanged event will bubble up to the TextBox (where you can handle it). The event arguments include information about what changed, such as the VerticalChange (the amount that the control has scrolled vertically).

    private void TextBox_ScrollChanged(object sender, ScrollChangedEventArgs e) {
        System.Diagnostics.Debug.WriteLine(string.Format("************ {0}", e.VerticalChange));
    }