I will drag&drop a Button from one Stackpanel to another Stackpanel. The implementation still works, but I will that the Button is attached to the mouse cursor during the Drag Event as a visual Feedback.
I was looking for a solution a whole day, but I find only solutions with Canvas and not with Stackpanels.
Here is my Code:
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)
{
DataObject data = new DataObject();
data.SetData("Object", this);
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 (e.Handled == false)
{
Panel _panel = (Panel)sender;
UIElement _element = (UIElement)e.Data.GetData("Object");
if (_panel != null && _element != null)
{
Panel _parent = (Panel)VisualTreeHelper.GetParent(_element);
if (_parent != null)
{
_parent.Children.Remove(_element);
_panel.Children.Add(_element);
}
}
}
}