我試圖讓我的用戶交換兩個DevExpress圖表控件(儘管我相信幾乎任何控件都應該可以工作......),通過將其中一個拖動到另一個控件的頂部。我已經爲我的TabControl做了這個(爲了允許交換/移動標籤),但由於某種原因,我似乎在這裏丟失了一些東西,這些東西阻止了我對ChartControl的處理。我如何執行ChartControl的DragDrop /交換?
它「應該」在chartcontrol上畫一個灰色框,並允許用戶將它拖動到任何他們喜歡的位置,但我只是得到一個帶有條紋的黑色圓圈。
這是我寫到目前爲止的代碼,希望你們中的一個能夠發現錯誤,我可以停止拉我的頭髮! :)
private void ChartControlMouseMove(object sender, MouseEventArgs e)
{
// Handle Mouse move only if left button is pressed.
if (e.Button == MouseButtons.Left)
{
var chartControl = (ChartControl)sender;
// If the mouse moves outside the rectangle, start the drag.
if (!rectDragBoxFromMouseDown.Equals(Rectangle.Empty)
& !rectDragBoxFromMouseDown.Contains(e.X, e.Y))
{
Invalidate();
DoDragDrop(chartControl, DragDropEffects.Move);
CalcRectDragBox(e.X, e.Y);
Invalidate();
}
}
}
private void ChartControlMouseDown(object sender, MouseEventArgs e)
{
CalcRectDragBox(e.X, e.Y);
}
private void CalcRectDragBox(int x, int y)
{
// Remember the point where the mouse down occurred. The DragSize indicates
// the size that the mouse can move before a drag event should be started.
var dragSize = SystemInformation.DragSize;
// Create a rectangle using the DragSize, with the mouse position being
// at the center of the rectangle.
rectDragBoxFromMouseDown = new Rectangle(
new Point(x - (dragSize.Width/2), y - (dragSize.Height/2)), dragSize);
}
private void ChartControlDragOver(object sender, DragEventArgs e)
{
var chartControl = (ChartControl)sender;
// get the control we are hovering over.
var hitInformation = chartControl.CalcHitInfo(chartControl.PointToClient(new Point(e.X, e.Y)));
if ((hitInformation != null))
{
//ChartHitInfo hoverTab = hitInformation;
if (e.Data.GetDataPresent(typeof(ChartControl)))
{
e.Effect = DragDropEffects.Move;
var dragTab = (ChartControl)e.Data.GetData(typeof(ChartControl));
if (dragTab != chartControl)
{
for (int i = 0; i < layoutControlGroupDashboard.Items.Count; i++)
{
var layoutControlItem = layoutControlGroupDashboard.Items[i] as LayoutControlItem;
if (layoutControlItem != null && layoutControlItem.Control == chartControl)
{
for (int j = 0; j < layoutControlGroupDashboard.Items.Count; j++)
{
var controlItem = layoutControlGroupDashboard.Items[j] as LayoutControlItem;
if (controlItem != null && controlItem.Control == dragTab)
{
if (!_ignoreNextDrag)
{
_ignoreNextDrag = true;
layoutControlGroupDashboard.BeginInit();
var layoutControlItemi = layoutControlGroupDashboard.Items[i] as LayoutControlItem;
if (layoutControlItemi != null)
{
Control tempControlI =
layoutControlItemi.Control;
var layoutControlItemj = layoutControlGroupDashboard.Items[j] as LayoutControlItem;
if (layoutControlItemj != null)
{
layoutControlItemi.BeginInit();
layoutControlItemj.BeginInit();
Control tempControlJ =
layoutControlItemj.Control;
layoutControlItemi.Control =
null;
layoutControlItemj.Control =
null;
layoutControlItemi.Control =
tempControlJ;
layoutControlItemj.Control =
tempControlI;
layoutControlItemi.EndInit();
layoutControlItemj.EndInit();
}
}
layoutControlGroupDashboard.EndInit();
break;
}
else
{
_ignoreNextDrag = false;
break;
}
}
}
}
}
}
}
}
else
{
e.Effect = DragDropEffects.None;
}
}
同樣,這個想法是允許交換控制用戶周圍只需點擊點擊拖動周圍的事物......希望這只是一些簡單的我失蹤,但我不能看到它爲了我的生活!
編輯:這是我嘗試(第一次將我的圖表面板...)
Panel panel = new Panel();
panel.Name = Guid.NewGuid().ToString();
panel.Controls.Add(chartControl);
var dashboardItem = new LayoutControlItem(layoutControlDashboard, panel)
{
Padding = new DevExpress.XtraLayout.Utils.Padding(0),
Spacing = new DevExpress.XtraLayout.Utils.Padding(0),
SizeConstraintsType = SizeConstraintsType.Custom
};
您的代碼可以正常工作,無需任何修改。您是否啓用了圖表控件的AllowDrop選項? – Uranus
@Uranus他們互換地點嗎?我沒有啓用「AllowDrop」,但即使啓用它,它們也不會更改位置。如果他們爲你換了什麼類型的控制,你有他們?我有一個LayoutControl中的LayoutControlItems。 – Faraday
是的,他們這樣做。我已經把控件放在不同的面板上來測試你的代碼,因爲它改變了父窗口來翻轉控件。如果您將圖表控件放置在LayoutControl中,則此代碼不起作用,因爲在此情況下,LayoutControl是兩者的父級。相反,需要移動佈局項目。 – Uranus