【问题标题】:How to move controls on Canvas at runtime in WPF?如何在 WPF 运行时在 Canvas 上移动控件?
【发布时间】:2012-10-02 13:54:51
【问题描述】:

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

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

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

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

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

有人知道吗?

提前致谢

【问题讨论】:

  • 那么您可以在运行时添加标签吗?显示有效的代码和无效的代码。用一些具体的代码更容易回答。
  • 它需要如何移动?您是否只想将位置设置为其他值?您想重新订购商品吗?什么决定了每个控件的新位置?
  • 地点比移动更好。当我读到标题时,我以为你想要动画。

标签: c# wpf


【解决方案1】:

可以通过

获取和设置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

【讨论】:

    【解决方案2】:

    WPF:Windows 演示基础 | C Sharp : 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 用于执行移动控件的逻辑;


    【讨论】:

    • [!] 确保您的项目参考包含在以下程序集中:PresentationCore;演示框架;系统;系统.Windows; System.Xaml; WindowsBase;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多