views:

662

answers:

2

Context:

There's an application where you draw things on canvas. Where user clicks there's a black dot, for example. But handling that events raised by canvas in event handlers in main window code is just ugly for me. So I wrote a class which methods mirror canvas mouse events and I call those methods inside event handler.

public partial class Window1 : Window
{
    DrawingTool drawTool = new DrawingTool();
    public Window1()
    {
        InitializeComponent();            
    }

    private void drawingCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        drawTool.OnMouseLeftButtonDown(e);
    }
}

Nice, but I want more...

Question:

Is it possible to handle events raised by canvas in my DrawingTool class directly without having defined canvas event handlers. I'd like to keep my Window1 code clean and focused on window stuff and move events handling completely into my classes.

I thought about deriving DrawingTool from FrameworkElement, override OnRender and draw it transparently over the canvas so user's click seemed to be made on canvas but in fact would be raised in DrawingTool and handled internally. This approach works with drawing functionality (after drawing is done drawn object is added to canvas children and removed from DrawingTool children) but when it comes to selection mode or others there are problems with hit testing of elements for example.

So, I'll be grateful for any suggestion how to solve my problem or explanation why this is a stupid idea at all :)

+1  A: 

Just expose the private control fields as public properties and when you have an instance of Window1 you can just do

Window1.DrawingCanvas.MouseLeftButtonDown += YourHandlerMethod;
Strelok
That's pretty straightforward solution and I didn't think about it. I was looking in event setters placed in style or some clever delegate voodoo but as usual okham's razor came in handy :) Thx.
grapkulec
A: 

I'm not sure whether I got your problem right, and I've only played with WPF a little, but how about creating a user control that contains your canvas and handling the mouse and drawing there? This could be named DrawingControl or the like.

That way you'd be able to have the drawing code contained in the class that represents the control. You might want to give the new user control a property that allows access to the "internal" canvas.

Thorsten Dittmar
That would be the way if I wanted to make my own drawing control but I don't :)
grapkulec
Ah, OK. See? I didn't get your problem right :-D
Thorsten Dittmar