【问题标题】:WPF Drag&Drop, DragOverWPF 拖放、DragOver
【发布时间】:2015-01-29 10:57:20
【问题描述】:

我想将一个按钮从一个Stackpanel 拖放到另一个Stackpanel。该实现仍然有效,但我希望在拖动事件期间将按钮附加到鼠标光标作为视觉反馈。

我整天都在寻找解决方案,但我只找到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);
        }
    }

Window.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);

                }

            }
        }
    }

【问题讨论】:

  • 您需要在某个父容器上添加一个装饰器。您也许可以将画布放入该装饰器中并获得您所追求的效果。谷歌这个:wpf dragadorner

标签: c# wpf drag-and-drop


【解决方案1】:
void StackPanel_DragEnter(object sender, System.Windows.DragEventArgs e)
{
     e.Effects = System.Windows.DragDropEffects.Copy;
}

您可以使用此事件并对其进行修改,使其适用于您的拖放事件..

【讨论】:

    猜你喜欢
    • 2015-12-17
    • 1970-01-01
    • 2017-02-05
    • 2020-06-19
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    相关资源
    最近更新 更多