如何从另一个类添加Canvas事件? WPF C#

时间:2014-01-12 16:04:11

标签: c# wpf

我有一个MainWindow类,这是应用程序的主窗口。我在MainWindow类下面创建了另一个类,我需要向在MainWindow中创建的Canvas添加一个事件。我想要添加为事件的方法也在MainWindow中。

public partial class MainWindow : Window
{
     public void Moving(object sender, MouseEventArgs e)
     { 
        bla bla;
     }

      public Canvas Getcanvas
      {
          get
          {
             return canvas;
          }
      }
}


public class Ka
{
      public Ka()
       {
            MainWindow.Getcanvas.MouseMove += new MouseEventHandler(//HERE!!! I DONT KNOW WHAT GOES HERE, here should probably be MainWindow.Moving but I get error "An object reference.."
       }

}

请帮助我!

1 个答案:

答案 0 :(得分:0)

var mainWindowInstant = (MainWindow)App.Current.MainWindow;
mainWindowInstant.Getcanvas.MouseMove += new MouseEventHandler(...);