WPF 精修篇 拖拽 DragDrop

原文:WPF 精修篇 拖拽 DragDrop

WPF 实现拖拽

效果

WPF  精修篇 拖拽 DragDrop

  1. <Grid>
  2. <Grid.ColumnDefinitions>
  3. <ColumnDefinition Width="197*"/>
  4. <ColumnDefinition Width="209*"/>
  5. <ColumnDefinition Width="111*"/>
  6. </Grid.ColumnDefinitions>
  7. <WrapPanel x:Name="accept" HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="207" Grid.Column="1" Margin="2,0,0,0" Background="#FFE7FFE5" Drop="WrapPanel_Drop" AllowDrop="True" />
  8. <WrapPanel x:Name="send" HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="197" Background="#FFFFEDCD" MouseLeftButtonDown="WrapPanel_MouseLeftButtonDown">
  9. <Label Content="Label1" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
  10. <Label Content="Label2" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
  11. <Label Content="Label3" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
  12. <Label Content="Label4" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
  13. <Label Content="Label5" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
  14. <Label Content="Label6" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
  15. </WrapPanel>
  16. </Grid>
  1. private void WrapPanel_Drop(object sender, DragEventArgs e)
  2. {
  3. var item = e.Data;
  4. object data = item.GetData(item.GetFormats()[0]);
  5. if (data is UIElement)
  6. {
  7. send.Children.Remove(data as UIElement);
  8. accept.Children.Add(data as UIElement);
  9. }
  10. }
  11. private void WrapPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  12. {
  13. object item = e.Source;
  14. if (item is UIElement)
  15. {
  16. DragDrop.DoDragDrop(item as UIElement, item, DragDropEffects.Move);
  17. }
  18. }

 

accept 一方的控件 需要加上  AllowDrop="True" 允许接收drop的数据

WPF 精修篇 拖拽 DragDrop

上一篇:log4j.xml简单配置实现在控制台打印sql执行语句【加注释】


下一篇:SOAPUI使用教程-测试JDBC数据库