1

我正在尝试移动在运行时生成的控件并希望在 WPF 中的画布上移动。

我想构建像报表生成器这样的应用程序(不完全像水晶报表)。但想要构建一些我可以将控件(Label,TextBox,RichTextBox,Image Control等)放在画布(WPF)上然后在画布上移动的东西。

我无论如何都在寻找画布上的移动控件,并在画布上捕获位置,以便我可以生成(XPS 或 PDF)格式的报告。我制作报告没有问题。

我只面临在运行时移动控制和添加的问题TextBlockImageControl

我真的需要这方面的指导。

有人知道吗?

提前致谢

4

2 回答 2

2

您可以通过以下方式获取和设置 Canvas 上元素的左坐标

Canvas.GetLeft(element) and Canvas.SetLeft(element, number)

还有

Canvas.GetTop/Canvas.SetTop, Canvas.GetRight/Canvas.SetRight, Canvas.GetBottom/Canvas.SetBottom and Canvas.GetZIndex/Canvas.SetZIndex
于 2012-10-02T14:10:21.933 回答
0

WPF:Windows 演示基础 | C 夏普:C#

使用( System.Windows.Thickness)移动控件


//Variables ----------------------
static System.Boolean MMove = false;
static System.Windows.Point MPoint1;
static System.Windows.Point MPoint2;
static System.Windows.Controls.Control MyControl = null;
static System.Windows.Controls.Canvas MyCanvas = null;

[?]在应用程序中完成移动控制所需的静态变量;
--- MyCanvas必须在您的应用程序启动时指定;(或使用前的任何时候)


//MouseEvents --------------------
yourControl.MouseDown += new System.Windows.Input.MouseButtonEventHandler(MouseDown);
yourControl.MouseUp += new System.Windows.Input.MouseButtonEventHandler(MouseUp);
yourControl.PreviewMouseMove += new System.Windows.Input.MouseEventHandler(MouseMove);

[?]要分配给您的控件的鼠标事件;


//MouseDown EventHandler ---------
static void MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    if (sender is UserControl1)
    {
        //get control clicked
        MyControl = (UserControl1)sender;

        //get coordinates
        MPoint1 = e.GetPosition(MCanvas);
        MPoint2 = e.GetPosition(MyControl);

        //update coordinates
        MPoint1.X -= MyControl.Margin.Left + MPoint2.X;
        MPoint1.Y -= MyControl.Margin.Top + MPoint2.Y;

        //prevent mouse loosing focus of the control
        System.Windows.Input.Mouse.Capture(MyControl);

        //enable move indicator
        MMove = true;
    }
}

//MouseMove EventHandler ---------
static void MouseMove(object sender, System.Windows.Input.MouseEventArgs e) {
    if (sender is UserControl1) {
        //check move indicator
        if (MMove == true)
        {
            //get control moved
            MyControl = (UserControl1)sender;

            //get container pointer
            var currentPoint = e.GetPosition(MCanvas);

            //check which mouse button is pressed
            if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
            {
                //update control margin thickness
                MyControl.Margin = new System.Windows.Thickness(
                    currentPoint.X - MPoint1.X - MPoint2.X,
                    currentPoint.Y - MPoint1.Y - MPoint2.Y,
                    MyControl.Margin.Right,
                    MyControl.Margin.Bottom
                );
            }
        }
    }
}

//MouseUp EventHandler ---------
static void MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    MMove = false;
    MyControl = null;
    System.Windows.Input.Mouse.Capture(null);
}

[?] EventHandler用于执行移动控件的逻辑;


于 2019-08-10T21:59:14.540 回答