2014-01-14 194 views
2

XAML绘图画布线

<Canvas Name="canvas" MouseDown="canvas_MouseDown" MouseUp="canvas_MouseUp" /> 

C#

Point P1; 
private void canvas_MouseDown(object sender, MouseButtonEventArgs e) 
{ 
    P1 = e.GetPosition(canvas); 
} 

private void canvas_MouseUp(object sender, MouseButtonEventArgs e) 
{ 
    var P2 = e.GetPosition(canvas); 
    canvas.Children.Add(new Line() 
    { 
     X1 = P1.X, 
     Y1 = P1.Y, 
     X2 = P2.X, 
     Y2 = P2.Y, 
     StrokeThickness = 3, 
     Stroke = new SolidColorBrush { Color = Colors.Blue } 
    }); 
} 

当调试鼠标向上或向下的鼠标事件永远不会被调用,无论是。任何想法为什么?

+2

您需要在画布上的背景下,尝试加入一个透明, –

+2

@JohanLarsson工作,非常感谢.. .i甚至在几个小时后都不知道,请添加一个答案 – user1492051

回答

3

您需要在画布上的背景为它捕捉老鼠,尝试添加一个透明:

<Canvas Name="canvas" 
     MouseDown="canvas_MouseDown" 
     MouseUp="canvas_MouseUp" 
     Background="Transparent"/>