2014-01-14 222 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"/>