2012-06-22 28 views
0

我試圖讓我的用戶交換兩個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 
            }; 
+1

您的代碼可以正常工作,無需任何修改。您是否啓用了圖表控件的AllowDrop選項? – Uranus

+0

@Uranus他們互換地點嗎?我沒有啓用「AllowDrop」,但即使啓用它,它們也不會更改位置。如果他們爲你換了什麼類型的控制,你有他們?我有一個LayoutControl中的LayoutControlItems。 – Faraday

+1

是的,他們這樣做。我已經把控件放在不同的面板上來測試你的代碼,因爲它改變了父窗口來翻轉控件。如果您將圖表控件放置在LayoutControl中,則此代碼不起作用,因爲在此情況下,LayoutControl是兩者的父級。相反,需要移動佈局項目。 – Uranus

回答

1

下面是修改ChartControlDragOver方法,工作情況下,ChartControl被放置在LayoutControl:

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) return; 
     InsertType insertType = InsertType.Left; 
     Point hitPoint = chartControl.Parent.PointToClient(new Point(e.X, e.Y)); 
     if (dragTab.Bounds.Left < hitPoint.X && dragTab.Bounds.Right > hitPoint.X) { 
      if (dragTab.Bounds.Top > hitPoint.Y) 
       insertType = InsertType.Top; 
      else if (dragTab.Bounds.Bottom < hitPoint.Y) 
       insertType = InsertType.Bottom; 
     } else if (dragTab.Bounds.Right < hitPoint.X) 
      insertType = InsertType.Right; 
     else if (dragTab.Bounds.Left > hitPoint.X) 
      insertType = InsertType.Left; 
     LayoutControl layout = (LayoutControl)chartControl.Parent; 
     layout.GetItemByControl(dragTab).Move(layout.GetItemByControl(chartControl), insertType); 
    } 
} else { 
    e.Effect = DragDropEffects.None; 
} 

}

+0

對於橫向移動,這確實有很好的工作效果。所以如果我有兩個控制器,一個在另一個之上或者在另一個的旁邊,這個工作真的很好! :)我遇到的問題是,如果用戶使用這些代碼拖動這些代碼,那麼在用戶可能遇到的各種佈局的情況下,他們最終會陷入被卡住的狀態(移動的東西什麼也不做)。我已經把一個非常簡單的示例應用程序扔給你看,去tab2並點擊添加幾次,然後回到tab1並拖動東西一分鐘......我真的希望他們交換位置。 – Faraday

+0

我已經投票贊成並感謝您的努力,但不能給你答案,因爲它不能正常工作...我原本以爲這將是該圖表將有一個LayoutControlItem的父母,我可以切換,它會工作! :S 以下是我的示例應用程序:http://www9.zippyshare.com/v/18361378/file.html – Faraday

+0

如果將圖表控件放在面板中,並將這些面板放在佈局控件中,則可以使用與TabControl一起使用的代碼。 – Uranus