我有一个分为3列的网格。第一个包含StackPanel,第二个包含Canvas,而不是StackPanel。当我的窗口加载时,stackpanels填充了文本块。如何将这些文本块拖放到画布上?
我尝试使用mouseDown和mouseMove事件但是失败了......
的Xaml:
<Window x:Class="StackDragDrop.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="800" Width="1000">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical" Name="APanel" HorizontalAlignment="Center"/>
<Canvas Name="MyCanvas" Grid.Column="1" AllowDrop="True"/>
<StackPanel Orientation="Vertical" Name="BPanel" Grid.Column="2" HorizontalAlignment="Center"/>
</Grid>
代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 3; i++)
{
TextBlock Block = new TextBlock();
Block.FontSize = 25;
Block.Text = (i + 1).ToString();
Block.MouseMove += Block_MouseMove;
APanel.Children.Add(Block);
}
for (int i = 3; i < 6; i++)
{
TextBlock Block = new TextBlock();
Block.FontSize = 25;
Block.Text = (i + 1).ToString();
Block.MouseMove += Block_MouseMove;
BPanel.Children.Add(Block);
}
}
void Block_MouseMove(object sender, MouseEventArgs e)
{
DragDrop.DoDragDrop((sender as TextBlock).Parent, sender, DragDropEffects.Move);
}
}