0

Unable to work with MouseWheel event in avalonedit TextEditor.

WPF :

<avalonedit:TextEditor x:Name="txEditor" Margin="18" FontFamily="Consolas" FontSize="16px" ShowLineNumbers="True" MouseWheel="txEditor_MouseWheel" />

c# :

private void txEditor_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
    MessageBox.Show(e.Delta.ToString());
}

the event not firing at all.

Dany Maor
  • 2,391
  • 2
  • 18
  • 26

2 Answers2

2

Some Events are already catched by the AvalonEdit UserControl. So they simply don't reach your scope!

But there maybe a simple solution for that, you should be able to use the tunnelling event.


PreviewMouseWheel example:

WPF:

<avalonedit:TextEditor x:Name="txEditor" Margin="18" FontFamily="Consolas" FontSize="16px" ShowLineNumbers="True" PreviewMouseWheel="txEditor_PreviewMouseWheel" />

C#:

    private void txEditor_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        MessageBox.Show(e.Delta.ToString());
    }
Momo
  • 456
  • 5
  • 22
0

add the following to mousewheel event of AvalonEdit UserControl

    //add MouseWheelEventHandler to avaloneditor.TextArea.MouseWheel
    private void InitializeMethod()
    {
        :
        this.TextArea.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(textEditor_MouseWheel);
        :
    }


    
    private void textEditor_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
    {
        if (e.Delta != 0)
        {
            //your code here
        }
        //e.Handled value is important, if not added, default value is true
        //false: your code then do avaloneditor mousewheel code
        //true: just do your code then skip avaloneditor mousewheel default code
        e.Handled = false;   
    }

You can also use scrollchange as this works better on my case

this.TextArea.TextView.ScrollOffsetChanged += textEditor_ScrollChanged;

private void textEditor_ScrollChanged(object sender, EventArgs e)
{
//your code here
}
ray
  • 15
  • 4