2015-01-29 4 views
5

Я хочу перетащить & Отбросьте кнопку от одного Stackpanel к другому Stackpanel. Реализация все еще работает, но я хочу, чтобы кнопка была привязана к курсору мыши во время события перетаскивания как визуальная обратная связь.WPF Drag & Drop, DragOver

Я искал решение целый день, но я нашел только решения с Canvas, а не с Stackpanels.

Вот мой код:

Button.xaml

<UserControl x:Class="LisaBeispiel2.Controls.ImageButton" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Name="IB" 
    AllowDrop="True"> 


<Grid> 
    <Button Margin="3 0 3 3" 
      VerticalAlignment="Top" 
      HorizontalAlignment="Left" 
      IsEnabled="{Binding IsButtonEnabled, ElementName=IB}"> 
     <StackPanel Orientation="Vertical"> 
      <Image Source="{Binding ElementName=IB, Path=Image}" 
        Width="35" 
        Height="35" 
        Margin="8" /> 
      <TextBlock Text="{Binding ElementName=IB, Path=Text}" 
         Foreground="White" 
         FontSize="10" 
         FontFamily="Arial" 
         TextWrapping="Wrap" 
         TextAlignment="Center" /> 
     </StackPanel> 
    </Button> 

</Grid> 

Button.xaml.cs

protected override void OnMouseMove(MouseEventArgs e) 
    { 
     base.OnMouseMove(e); 
     if (e.LeftButton == MouseButtonState.Pressed) 
     { 
      // Package the data into a Dataobject 
      DataObject data = new DataObject(); 

      data.SetData("Object", this); 

      // Inititate the drag-and-drop operation with 3 Parameter: dragSource, data, allowedEffects 
      DragDrop.DoDragDrop(this, data, DragDropEffects.All); 
     } 
    } 

окно .xaml

<StackPanel Orientation="Horizontal" 
       Drop="panel_Drop"> 
    <controls:ImageButton 
    Image="/LisaBeispiel2;component/Images/Icons/Rotate.png" 
    Text="Rotate"/> 
    <controls:ImageButton 
    Image="/LisaBeispiel2;component/Images/Icons/Zoom_Pan.png" 
    Text="Zoom/Pan"/> 
    </StackPanel> 
     <StackPanel Orientation="Horizontal" 
        Drop="panel_Drop"> 
    <controls:ImageButton 
    Image="/LisaBeispiel2;component/Images/Icons/ButtonPlatzhalter.png" 
    Text="Add"/> 
    <controls:ImageButton 
    Image="/LisaBeispiel2;component/Images/Icons/ButtonPlatzhalter.png" 
    Text="Add"/> 
    </StackPanel> 
    </StackPanel> 
    </StackPanel> 

Window.xaml.cs

private void panel_Drop(object sender, DragEventArgs e) 
    { 
     // If an element in the panel has already handled the drop, 
     // the panel should not also handle it. 
     if (e.Handled == false) 
     { 
      Panel _panel = (Panel)sender; 
      UIElement _element = (UIElement)e.Data.GetData("Object"); 

      if (_panel != null && _element != null) 
      { 
       // Get the panel that the element currently belongs to, 
       // then remove it from that panel and add it the Children of 
       // the panel that its been dropped on. 
       Panel _parent = (Panel)VisualTreeHelper.GetParent(_element); 

       if (_parent != null) 
       { 
        _parent.Children.Remove(_element); 
        _panel.Children.Add(_element); 

       } 

      } 
     } 
    } 
+0

Вы вам понадобится adorner в каком-то родительском контейнере. Вы можете положить холст в этого adorner и получить эффект, который вы после. Google это: wpf dragadorner – Brannon

ответ

0
void StackPanel_DragEnter(object sender, System.Windows.DragEventArgs e) 
{ 
    e.Effects = System.Windows.DragDropEffects.Copy; 
} 

вы могли бы использовать это событие и изменить его, чтобы он работает с перетащить & случае падения ..