簡體   English   中英

C#WPF-自定義控件拖放(Visual Studio樣式)

[英]C# WPF - Custom Control drag and drop (Visual Studio style)

假設我如何使我的自定義控件在網格上可拖動和拖放?

我想將面板(自定義控件)拖放到屏幕上的某個位置,以網格中的最佳情況為例,例如在Visual Studio中是如何完成的,您可以抓住解決方案資源管理器,比如說將其拖放到某個位置,但是如何我到底要這么做嗎?

我發現這篇文章的結論與此非常相似。 區別在於規模。

以下示例將交換父容器

    int i = 0;
    void swapLocations()
    {

        foreach(var formObject in objList) //objList == a list or array on all objects you want to move from one container to another
        {

            if (i % 2 == 0)
            {

                // catch current position             
                Point moveLocation = new Point(formObject.Location.X + formObject.Parent.Location.X,formObject.Location.Y + formObject.Parent.Location.Y);

                // remove this object
                formObject.Parent.Controls.Remove(formObject);

                // add this object to the form
                this.Controls.Add(formObject);

                 // set location
                formObject.Location = moveLocation;

                formObject.SendToBack();
            }
            else
            {
                formObject.BringToFront();
            }
        }
        ++i;
    }

您需要構建項目,然后當您在XAML設計器中時,它將在工具箱中自動可用。 就像常用控件一樣。

有關在運行時進行拖放的信息,請查看WPF官方文檔 我還建議您查看GitHub中的GongSolutions.WPF.DragDrop庫,它是開源的,因此,如果它提供的功能無法滿足您的要求,您可以了解他們如何實現它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM