2016-05-24 34 views
3

我對C#和WPF很新,並且會創建一個WPF應用程序,該應用程序使用按鈕繪製形狀。然後形狀需要能夠在畫布上移動。當我在XAML中創建一個形狀時,它會移動。但是我無法獲得由按鈕創建的那個移動。任何人都可以請協助?以下是我正在使用的XAML和代碼。如何移動WPF中畫布上的按鈕創建的形狀?

XAML:

<Window x:Class="All_test.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Canvas x:Name="canvas" > 
    <Button Content="Button" Canvas.Left="250" Canvas.Top="260" Width="75" Click="Button_Click_1" /> 
    <Rectangle x:Name="rect" 
       Height="100" Width ="100" Fill="red" 
       MouseLeftButtonDown="rect_MouseLeftButtonDown" 
      MouseLeftButtonUp="rect_MouseLeftButtonUp" 
      MouseMove="rect_MouseMove" 
       Canvas.Left="342" Canvas.Top="110" /> 
</Canvas> 

這是我使用移動XAML中繪製的紅色正方形的代碼。我怎樣才能做到按鈕創建的綠色相同?

public partial class MainWindow : Window 
{ 
    private bool _isRectDragInProg; 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 


    private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     Rectangle rect = new Rectangle(); 
     rect.Fill = new SolidColorBrush(Colors.Green); 
     rect.Stroke = new SolidColorBrush(Colors.Black); 
     rect.Height = 100; 
     rect.Width = 100; 
     rect.StrokeThickness = 4; 
     canvas.Children.Add(rect); 
     InitializeComponent(); 

    } 


    private void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     _isRectDragInProg = true; 
     rect.CaptureMouse(); 
    } 


    private void rect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     _isRectDragInProg = false; 
     rect.ReleaseMouseCapture(); 
    } 


    private void rect_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (!_isRectDragInProg) return; 

     // get the position of the mouse relative to the Canvas 
     var mousePos = e.GetPosition(canvas); 

     // center the rect on the mouse 
     double left = mousePos.X - (rect.ActualWidth/2); 
     double top = mousePos.Y - (rect.ActualHeight/2); 
     Canvas.SetLeft(rect, left); 
     Canvas.SetTop(rect, top); 
    } 

回答

1

您應該綁定鼠標事件此Rectangle

private void Button_Click_1(object sender, RoutedEventArgs e) 
{ 
    Rectangle rect = new Rectangle(); 
    rect.Fill = new SolidColorBrush(Colors.Green); 
    rect.Stroke = new SolidColorBrush(Colors.Black); 
    rect.Height = 100; 
    rect.Width = 100; 
    rect.StrokeThickness = 4; 
    // here 
    rect.MouseLeftButtonDown += rect_MouseLeftButtonDown; 
    rect.MouseLeftButtonUp += rect_MouseLeftButtonUp; 
    rect.MouseMove += rect_MouseMove; 

    canvas.Children.Add(rect); 
    // InitializeComponent(); <--- lose the InitializeComponent here, that's should only be called ones.. (in the constructor) 
} 

建議:

  • 使用sender參數來獲得當前Rectangle
  • 您可能會丟失_isRectDragInProg boolean ...請改用IsMouseCaptured屬性。

例如:

private void rect_MouseMove(object sender, MouseEventArgs e) 
{ 
    var rect = (Rectangle)sender; 

    if (!rect.IsMouseCaptured) return; 

    // get the position of the mouse relative to the Canvas 
    var mousePos = e.GetPosition(canvas); 

    // center the rect on the mouse 
    double left = mousePos.X - (rect.ActualWidth/2); 
    double top = mousePos.Y - (rect.ActualHeight/2); 
    Canvas.SetLeft(rect, left); 
    Canvas.SetTop(rect, top); 
} 
+0

謝謝你,好先生!需要進行一些調整,但總的來說,這幫助我瞭解了這個想法。這些建議也受到讚賞。 :) – Greengored