2017-10-03 2 views
-2

현재 눈금이있는 폼에서 작업하고 있습니다. 컨트롤이 그리드에 동적으로 추가됩니다. 추가되는 컨트롤의 유일한 유형은 버튼입니다. 사용자가 컨트롤을 원하는 셀에 배치하려면 "끌어서 놓기"를 사용해야합니다. 또한 특정 컨트롤을 배치 할 셀이 바쁜 경우 컨트롤이 단순히 장소를 전환하기를 원합니다. 내 우선 순위는 appropriate cell의 끌어다 놓기 컨트롤입니다.격자 내에서 동적으로 추가 된 컨트롤 이동 - WPF

private void BtTable_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     mouseOffset = Mouse.GetPosition(this); 
     CaptureMouse(); 
     //GridContent.Parent = this; 
    } 
MouseLeftButtonDown

나는 다음과 같은 노력하지만

변수

Point mouseOffset; 
Point position; 

작동하지 않습니다 515,

MouseMove 명령어

private void BtTable_MouseMove(object sender, MouseEventArgs e) 
     { 
      if (IsMouseCaptured) 
      { 
       Point mouseDelta = Mouse.GetPosition(this); 
       mouseDelta.Offset(-mouseOffset.X, -mouseOffset.Y); 
       Margin = new Thickness(
        Margin.Left + mouseDelta.X, 
        Margin.Top + mouseDelta.Y, 
        Margin.Right - mouseDelta.X, 
        Margin.Bottom - mouseDelta.Y); 
      } 
     } 

하는 MouseLeftButtonUp

private void BtTable_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     Grid.SetRow(this, (int)position.Y); 
     Grid.SetColumn(this, (int)position.X); 
     Margin = new Thickness(); 
    } 

와과 GetPosition 방법

public void getPosition(UIElement element, out int col, out int row) 
    { 
     Button newBtn = element as Button; 
     Grid grid = newBtn.Parent as Grid; 
     var point = Mouse.GetPosition(element); 
     row = 0; 
     col = 0; 
     double accumulatedHeight = 0.0; 
     double accumulatedWidth = 0.0; 

     // calc row mouse was over 
     foreach (var rowDefinition in grid.RowDefinitions) 
     { 
      accumulatedHeight += rowDefinition.ActualHeight; 
      if (accumulatedHeight >= point.Y) 
       break; 
      row++; 
     } 

     // calc col mouse was over 
     foreach (var columnDefinition in grid.ColumnDefinitions) 
     { 
      accumulatedWidth += columnDefinition.ActualWidth; 
      if (accumulatedWidth >= point.X) 
       break; 
      col++; 
     } 
    } 

여기에 무엇이 잘못되었으며 왜 드래그 앤 드롭이 작동하지 않는지 알 수 있습니까? 미리 감사드립니다.

답변

-1

귀하의 솔루션이 권장됩니다. MouseLeftButtonDown + MouseMove를 사용하여 드래그 앤 드롭을 감지하면 올바르게 수행 할 수 있습니다. 나는 훨씬 더 간단하게 할 수 없다고 생각한다.

사용하기 쉽게하려면 별도의 패키지에 넣으십시오. 또는 Gong WPF Drag&Drop과 같은 기존 패키지를 사용하십시오.

+0

내 솔루션에 가까움,하지만 여전히 작동하지 않으며 문제가 어디에 있는지 알 수 없습니다. –

+0

질문에 해당 정보를 추가해야합니다. 당신은 당신의 해결책이 효과가 없다고 말하지 않았습니다. 대신에 "어떻게하면 더 간단하고 우아한 방법으로 이것을 할 수 있을지 생각해보십시오. 미리 감사드립니다." – Picard

관련 문제