2016-04-18 33 views
0

我試圖在拖動可拖動項目時創建跟隨鼠標指針的彈出窗口。WPF DragDrop.GiveFeedback不連續發射

下剪斷打開在鼠標指針彈出,但我靠GiveFeedback委託,讓我更新的彈出位置(即,如果指針移動)

private void Members_DragOver(object sender, DragEventArgs e) 
{ 
    dragPopup.DataContext = DraggedItem; 
    var mousePoint = Mouse.GetPosition(this); 
    dragPopup.HorizontalOffset = mousePoint.X + this.Left + 10; 
    dragPopup.VerticalOffset = mousePoint.Y + this.Top + 10; 
    dragPopup.IsOpen = true; 
} 

private void Members_GiveFeedback(object sender, GiveFeedbackEventArgs e) 
{ 
    if (dragPopup.IsOpen) 
    { 
     var mousePoint = Mouse.GetPosition(this); 
     dragPopup.HorizontalOffset = mousePoint.X + this.Left + 10; 
     dragPopup.VerticalOffset = mousePoint.Y + this.Top + 10; 
     dragPopup.IsOpen = true; 
    } 
} 

而彈出的本身,這居住在我窗戶的主柵格中。

注意

我已經試過了MouseMovePreviewMouseMove處理過程中的位置,但這些事件是一個拖和下降過程中完全被忽略。

<Popup x:Name="dragPopup" Placement="MousePoint"> 
    <Border BorderThickness="2" Background="White" DataContext="{Binding}"> 
     <StackPanel Orientation="Horizontal" Margin="4,3,8,3"> 
      <TextBlock Text="{Binding FullName}" FontWeight="Bold" VerticalAlignment="Center" Margin="8,0,0,0" /> 
     </StackPanel> 
    </Border> 
</Popup> 

GiveFeedback代表火災只有一次(設置斷點,用5證實了這一點的隱私權|發佈廣告)。即使this MSDN article說:

這個事件在拖放操作過程中不斷上升。因此,您應該避免事件處理程序中的資源密集型任務。例如,每次引發GiveFeedback事件時,請使用緩存的遊標,而不是創建新的遊標。

委託人爲什麼只被解僱一次?

回答

1

你說得對,GiveFeedBack就是這樣做的。我確實設法在拖動過程中調用GiveFeedBack。顯然,GiveFeedBack中的鼠標位置與鼠標移動中的處理方式不同。我設法通過使用WinForms鼠標位置來解決此問題。添加參考System.Windows.Forms和System.Drawing中爲以下代碼:
的Xaml:

 <Grid GiveFeedback="Members_GiveFeedback"> 
       <Popup x:Name="dragPopup" Placement="MousePoint"> 
        <Border BorderThickness="2" Background="White" DataContext="{Binding}"> 
         <StackPanel Orientation="Horizontal" Margin="4,3,8,3"> 
          <TextBlock Text="Test" FontWeight="Bold" VerticalAlignment="Center" Margin="8,0,0,0" /> 
         </StackPanel> 
        </Border> 
       </Popup> 
       <StackPanel Orientation="Horizontal"> 
        <ListBox x:Name="sourcList" Height="50" 
        PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown" 
        PreviewMouseMove="sourcList_PreviewMouseMove" 
        AllowDrop="True" > 
         <ListBoxItem > source Item #1</ListBoxItem> 
       </ListBox> 

       <ListBox x:Name="droplist" Height="50" AllowDrop="True" Drop="droplist_Drop" > 
         <ListBoxItem >dest Item #2</ListBoxItem> 
        </ListBox> 
       </StackPanel> 
      </Grid> 

    private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
      { 
       var mousePoint = Mouse.GetPosition(this); 
       startPoint=mousePoint; 

       var point = GetMousePositionWindowsForms(); 
       var formsmousePoint = new Point(point.X, point.Y); 
       var pointfromscreen = dragPopup.PointFromScreen(formsmousePoint); 
       dragPopup.HorizontalOffset = pointfromscreen.X - 100; 
       dragPopup.VerticalOffset = pointfromscreen.Y - 100; 
       dragPopup.IsOpen = true; 
      } 

      private void sourcList_PreviewMouseMove(object sender, MouseEventArgs e) 
     { 
      ...     
     } 

    private void droplist_Drop(object sender, DragEventArgs e) 
     { 

      ... 

     } 

    private void Members_GiveFeedback(object sender, GiveFeedbackEventArgs e) 
     { 
      if (dragPopup.IsOpen) 
      { 
       var point=GetMousePositionWindowsForms(); 
       var mousePoint = new Point(point.X, point.Y); 
       var pointfromscreen=dragPopup.PointFromScreen(mousePoint); 
       dragPopup.HorizontalOffset = pointfromscreen.X-100; 
       dragPopup.VerticalOffset = pointfromscreen.Y-100; 
      } 
     } 

     public static Point GetMousePositionWindowsForms() 
     { 
      System.Drawing.Point point = System.Windows.Forms.Control.MousePosition; 
      return new Point(point.X, point.Y); 
     } 
+0

就是這樣,稍微調整一下,我就按照自己想要的方式設置了它。謝謝! – DerpyNerd

1

我剛收到你的彈出以下我的鼠標通過執行以下操作:

private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    var mousePoint = Mouse.GetPosition(this); 
    dragPopup.HorizontalOffset = mousePoint.X + this.Left + 10; 
    dragPopup.VerticalOffset = mousePoint.Y + this.Top + 10; 
    dragPopup.IsOpen = true; 
} 

private void Grid_MouseMove(object sender, MouseEventArgs e) 
{ 
    var mousePoint = Mouse.GetPosition(this); 
    dragPopup.HorizontalOffset = mousePoint.X + this.Left + 10; 
    dragPopup.VerticalOffset = mousePoint.Y + this.Top + 10;  
} 

然後在XAML側

<Grid MouseMove="Grid_MouseMove"> 
    ... 
    <ListBox Height="50" PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown" AllowDrop="True"> 
     <ListBoxItem> ListBox Item #1</ListBoxItem> 
    </ListBox> 

位置是有點過,但。

+0

位置應當由10因爲+ 10'mousePoint.X + this.Left + 10'的熄滅,你看到了什麼?我會試一試 – DerpyNerd

+0

太糟糕了。 'DoDragDrop'啓動後''MouseMove'不會被觸發。顯然,拖動不符合移動鼠標的要求。彈出窗口不跟隨鼠標,只是在開始拖動時停留在任何位置。 – DerpyNerd