Avalonedit以编程方式滚动 [英] Avalonedit scrollchanged programmatically
问题描述
我正在使用AvalonEdit显示带语法高亮的文件,我想处理ScrollChanged事件。我可以像这样注册xaml:
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:
<avalonedit:TextEditor ScrollViewer.ScrollChanged="TextEditor_ScrollChanged">
但是我想创建一个处理这个的子类,所以我不必每次都在Window的代码中这样做,但我一直无法弄清楚如何去做。
我尝试了什么:
以下尝试不起作用:
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;
}
我也尝试过关于stackoverflow的类似问题的建议:c# - 如何在MVVM中处理`ScrollViewer.ScrollChanged`事件? - Stack Overflow [ ^ ],但 UIHelper.FindChildren(this)
找不到ScrollViewer。
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.
推荐答案
添加路由事件处理程序:
Add a routed event handler:
public class MySubClass : TextEditor
{
public MySubClass()
{
AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(ScrollChanged));
}
这篇关于Avalonedit以编程方式滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!