赞
踩
下面代码只是简单示例,未使用MVVM模式编写
- <!-- 工具箱 -->
- <ListBox Grid.Column="0" x:Name="Toolbox">
- <ListBoxItem>Tool 1</ListBoxItem>
- <ListBoxItem>Tool 2</ListBoxItem>
- <!-- 添加更多工具 -->
- </ListBox>
- <Window.Resources>
- <Style TargetType="ListBoxItem">
- <EventSetter Event="PreviewMouseMove" Handler="ListBoxItem_PreviewMouseMove"/>
- </Style>
- </Window.Resources>
- private void ListBoxItem_PreviewMouseMove(object sender, MouseEventArgs e)
- {
- if(sender is ListBoxItem item)
- {
- DragDrop.DoDragDrop(item, item.Content, DragDropEffects.Copy);
- }
- }
- <!-- 流程图容器 -->
- <Canvas Grid.Column="1" x:Name="FlowChartCanvas" Background="White" AllowDrop="True" PreviewDragOver="FlowChartCanvas_PreviewDragOver" PreviewDrop="FlowChartCanvas_PreviewDrop">
- <!-- 流程图元素 -->
- </Canvas>
- private Point startPoint;
-
-
- private void FlowChartCanvas_PreviewDragOver(object sender, DragEventArgs e)
- {
- startPoint = e.GetPosition(FlowChartCanvas);
- }
- private void FlowChartCanvas_PreviewDrop(object sender, DragEventArgs e)
- {
- if (e.Data.GetDataPresent(DataFormats.StringFormat))
- {
- var tool = e.Data.GetData(DataFormats.StringFormat) as string;
- if (tool != null)
- {
- var element = new TextBlock
- {
- Text = tool,
- Background = Brushes.LightBlue,
- Width = 100,
- Height = 30
- };
- Canvas.SetLeft(element, startPoint.X);
- Canvas.SetTop(element, startPoint.Y);
- FlowChartCanvas.Children.Add(element);
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。