2015-01-29 2 views
5

& 버튼을 한 Stackpanel에서 다른 Stackpanel으로 드래그하고자합니다. 구현은 여전히 ​​작동하지만 시각적 피드백으로 끌기 이벤트 중에 Button을 마우스 커서에 연결하려고합니다.WPF 드래그 앤 드롭, 드래그 오버

나는 하루 종일 해결책을 찾고 있었지만 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 this : wpf dragadorner – Brannon

답변

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

이 이벤트를 사용할 수 있으며, 그것은 당신의 드래그 & 드롭 이벤트와 작동되도록