Click here to Skip to main content
15,499,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using AvalonEdit to display a file with syntax highlighting and I would like to handle the ScrollChanged event. I can register for it in xaml like so:
XAML
<avalonedit:TextEditor ScrollViewer.ScrollChanged="TextEditor_ScrollChanged">

I would however like to create a subclass that handles this, so I don't have to do this in the Window's code each time, but I've been unable to figure out how to do it.

What I have tried:

The following attempts didn't work:

public class MySubClass : TextEditor
{
  public MySubClass()
  {
    // Cannot convert type 'MyProgram.MySubClass' to 'System.Windows.Controls.ScrollViewer'
    ((ScrollViewer)this).ScrollChanged += ScrollChanged;

    // Cannot convert type 'ICSharpCode.AvalonEdit.Editing.TextArea' to 'System.Windows.Controls.ScrollViewer'
    ((ScrollViewer)TextArea).ScrollChanged += ScrollChanged;
  }


I've also tried the suggestion to a similar question on stackoverflow: c# - How to handle `ScrollViewer.ScrollChanged` event in MVVM? - Stack Overflow[^], but UIHelper.FindChildren(this) doesn't find a ScrollViewer.
Posted
Updated 3-Jul-18 3:11am

1 solution

Add a routed event handler:
C#
public class MySubClass : TextEditor
{
    public MySubClass()
    {
        AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(ScrollChanged));
    }
 
Share this answer
 
v3
Comments
[no name] 3-Jul-18 8:33am     CRLF
Thanks, this solution is perfect! [Edit] Your edit is even more elegant. I had to change it a little to get it working though:
AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(ScrollChanged));
Richard Deeming 3-Jul-18 8:43am    
Thanks - I've added that to the solution. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900