I am WPF newbie. In a WPF sample code I am looking at, there is a mouse event handler as follows:
namespace Recipe_04_15
{
public class DragCanvasControl : Canvas
{
...
static DragCanvasControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DragCanvasControl),
new FrameworkPropertyMetadata(typeof(DragCanvasControl)));
}
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
...
}
...
What I don’t understand is how the OnPreviewMouseLeftButtonDown method is wired to the mouse button down event. The XAML code does not have events specified?
The function is already wired in the UIElement class.
http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewmouseleftbuttondown.aspx
Since you inherit from the canvas you also inherit also from UIElement somewhere deeper.
DragCanvasControl => Canvas => Panel => FrameworkElement => UIElement
By overiding the OnPreviewMouseLeftButtonDown from UIElement you get access to this event.