使用WPF C#中的设置将子图像添加到画布

时间:2017-05-20 01:52:02

标签: c# wpf xaml canvas

我的主窗口中有一个画布控件,当单击一个按钮时,会将一个图像(从路径)添加到画布。

当添加这些图像时,我需要能够拖动它们,我可以通过xaml使用mouseMove等添加它们,但由于Click事件处理程序是在C#中完成的,我如何添加这些设置C#

使用xaml拖动等 - 不适合动态添加图像/形状

<!-- see below for drag functionality -->
<!-- 

//these are the xaml settings
//MouseLeftButtonDown = "shape_MouseLeftButtonDown" 
//MouseLeftButtonUp = "shape_MouseLeftButtonUp" 
//MouseMove = "shape_MouseMove"

-->

<Rectangle x:Name="semiTrailer" Fill="Red" Height="100" Stroke="Black" Width="319" HorizontalAlignment="Left" VerticalAlignment="Bottom" Canvas.Left="48" Canvas.Top="220" MouseLeftButtonDown="shape_MouseLeftButtonDown" MouseLeftButtonUp="shape_MouseLeftButtonUp" MouseMove="shape_MouseMove"/>

点击处理程序 - 如何添加移动,点击此处设置

private void addSemiOnly_Click(object sender, RoutedEventArgs e)
    {

        try {
            Image img;
            img = new Image();
            img.Source = new BitmapImage(new Uri(@"/NexansOlexProcessTools;component/Images/canvasImages/BTrailerOnlyTypeA.png", UriKind.Relative));

            //these are the xaml settings
            //MouseLeftButtonDown = "shape_MouseLeftButtonDown" 
            //MouseLeftButtonUp = "shape_MouseLeftButtonUp" 
            //MouseMove = "shape_MouseMove"

            //sets height works!
            img.Height = 60;

            //how to also add
            //these are the xaml settings
            //MouseLeftButtonDown = "shape_MouseLeftButtonDown" 
            //MouseLeftButtonUp = "shape_MouseLeftButtonUp" 
            //MouseMove = "shape_MouseMove"


            LayoutRoot.Children.Add(img);

        }
        catch (Exception drawError)
        {


        }
    }

要执行拖动功能的功能 - 适用于xaml添加的画布项

    private void shape_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        source = (UIElement)sender;
        Mouse.Capture(source);
        captured = true;
        x_shape = Canvas.GetLeft(source);
        x_canvas = e.GetPosition(LayoutRoot).X;
        y_shape = Canvas.GetTop(source);
        y_canvas = e.GetPosition(LayoutRoot).Y;
    }

    private void shape_MouseMove(object sender, MouseEventArgs e)
    {
        if (captured)
        {
            double x = e.GetPosition(LayoutRoot).X;
            double y = e.GetPosition(LayoutRoot).Y;
            x_shape += x - x_canvas;
            Canvas.SetLeft(source, x_shape);
            x_canvas = x;
            y_shape += y - y_canvas;
            Canvas.SetTop(source, y_shape);
            y_canvas = y;
        }
    }

    private void shape_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        Mouse.Capture(null);
        captured = false;
    }

1 个答案:

答案 0 :(得分:0)

让它在按钮单击处理程序中工作,请参阅下面添加所需事件的代码+设置Canvas.Left,Canvas.Top值的重要性

            Image img;
            img = new Image();
            img.Source = new BitmapImage(new Uri(@"/NexansOlexProcessTools;component/Images/canvasImages/BTrailerOnlyTypeA.png", UriKind.Relative));

            //Set Canvas vales - very important
            //Canvas.Left = "141" 
            //Canvas.Top = "116"
            Canvas.SetLeft(img, 150);
            Canvas.SetTop(img, 130);

            //set maouse events handlers for each item
            img.MouseMove += new MouseEventHandler(shape_MouseMove);
            img.MouseLeftButtonDown += new MouseButtonEventHandler(shape_MouseLeftButtonDown);
            img.MouseLeftButtonUp += new MouseButtonEventHandler(shape_MouseLeftButtonUp);
            //xaml settings
            //MouseLeftButtonDown = "shape_MouseLeftButtonDown" 
            //MouseLeftButtonUp = "shape_MouseLeftButtonUp" 
            //MouseMove = "shape_MouseMove"
            addSemiOnlyCount++;
            img.Tag = "addSemiOnlyId_" + addSemiOnlyCount;
            img.Name = "addSemiOnlyId_" + addSemiOnlyCount;
            img.ToolTip = "Item ID: addSemiOnlyId_" + addSemiOnlyCount;
            img.Height = 90;


            LayoutRoot.Children.Add(img);