C# WPF拖拽及;放下,德拉戈弗
我想将按钮从一个C# WPF拖拽及;放下,德拉戈弗,c#,wpf,drag-and-drop,C#,Wpf,Drag And Drop,我想将按钮从一个Stackpanel拖放到另一个Stackpanel。该实现仍然有效,但我希望在拖动事件期间将按钮作为视觉反馈附加到鼠标光标 我找了一整天的解决方案,但我只找到了使用Canvas的解决方案,而没有使用Stackpanels 这是我的密码: 按钮.xaml <UserControl x:Class="LisaBeispiel2.Controls.ImageButton" xmlns="http://schemas.microsoft.com/winfx/2006/xam
Stackpanel
拖放到另一个Stackpanel
。该实现仍然有效,但我希望在拖动事件期间将按钮作为视觉反馈附加到鼠标光标
我找了一整天的解决方案,但我只找到了使用Canvas
的解决方案,而没有使用Stackpanels
这是我的密码:
按钮.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>
<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
<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>
<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>
您可以使用此事件并对其进行修改,使其与拖放事件一起工作。您需要在某个父容器上安装装饰器。你可以把一块画布放进装饰物中,得到你想要的效果。谷歌搜索:wpf dragadorner
void StackPanel_DragEnter(object sender, System.Windows.DragEventArgs e)
{
e.Effects = System.Windows.DragDropEffects.Copy;
}