惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
量子位
美团技术团队
大猫的无限游戏
大猫的无限游戏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
N
Netflix TechBlog - Medium
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
月光博客
月光博客

博客园 - uxinxin

.NET10 中配置Swagger的记录 .NET10中通过appsettings.json配置文件获取信息的几个方法 学习使用Taurus的微服务群在.NET Framework4.8环境下的搭建 .net framework 中Owin 通过启动类 Startup.cs 使用 SignalR B站朝夕教育 【.NET9.0+WPF实战三类流程化业务逻辑控制】学习记录 【八】 B站朝夕教育 【.NET9.0+WPF实战三类流程化业务逻辑控制】学习记录 【七】 B站朝夕教育 【.NET9.0+WPF实战三类流程化业务逻辑控制】学习记录 【五】 B站朝夕教育 【.NET9.0+WPF实战三类流程化业务逻辑控制】学习记录 【四】 B站朝夕教育 【.NET9.0+WPF实战三类流程化业务逻辑控制】学习记录 【三】 B站朝夕教育 【.NET9.0+WPF实战三类流程化业务逻辑控制】学习记录 【二】 B站朝夕教育 【.NET9.0+WPF实战三类流程化业务逻辑控制】学习记录 .NET程序屏蔽一些无效蜘蛛的访问配置 asp.net 动态加载与卸载程序集的尝试(没有成功)欢迎解决的朋友留言告知 Framework 4.6.2下的SignalR使用,以及WPF下同步使用 react 16.8.4版本中的生命周期中的细节 在组件挂在时使用了定时器的处理——尚硅谷React教程(2022加更,B站超火react教程)学习笔记 CYQ.Data的XML操作的一些自己使用记录 IIS URL Rewrite将伪静态配置文件放在web.config外部 ASP.NET Core EF MVC 登录验证 一步步教你用html+div+css+js基于Jquery实现一套数字华容道游戏
B站朝夕教育 【.NET9.0+WPF实战三类流程化业务逻辑控制】学习...
uxinxin · 2024-12-04 · via 博客园 - uxinxin

播放地址:20241120-.NET9.0+WPF实战三类流程化业务逻辑控制-10_哔哩哔哩_bilibili

第14-15节 调整代码让拖拽到控制流程图里的模块产生位移

NodeModel新增两个属性X,Y记录控件的位置信息

1     public abstract class NodeModel: ObservableObject
2     {
3         public abstract string Name { get; set; }
4         private double x;
5         public double X { get { return x; } set => SetProperty(ref x, value); }
6         private double y;
7         public double Y { get { return y; } set => SetProperty(ref y, value); }
8         public abstract void Execute();
9     }

调整MainViewModel的ListBox_Drop方法里获取拖拽到的位置,并赋值给NodeModel的X,Y

 1     public partial class MainViewModel
 2     {
 3         public ObservableCollection<NodeModel> ProcessList { get; set; } = new ObservableCollection<NodeModel>();
 4 
 5         public void ListViewItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
 6         {
 7             DragDrop.DoDragDrop((DependencyObject)sender, (sender as ListViewItem).Tag, DragDropEffects.Copy);
 8         }
 9 
10         public void ListBox_Drop(object sender, DragEventArgs e)
11         {
12             string tag = e.Data.GetData(typeof(string)).ToString();
13             var point = e.GetPosition((IInputElement)sender);
14             //反射
15             //根据字符串获取类型
16             Type type = Assembly.GetEntryAssembly().GetType("WpfApp2." + tag);
17             //根据类型创建实例
18             NodeModel instance = (NodeModel)Activator.CreateInstance(type);
19             instance.X=point.X;
20             instance.Y=point.Y;
21             ProcessList.Add(instance);
22         }
23         [RelayCommand]
24         private void Execute()
25         {
26             foreach (var item in ProcessList)
27             {
28                 item.Execute();
29             }
30         }
31     }

调整界面MainView.xaml

重点注意ItemsControl需要设置属性Background,否则拖拽不了

其他代码详见内容

 1 <Window x:Class="WpfApp2.MainView"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:WpfApp2" xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
 7         mc:Ignorable="d"
 8         WindowStartupLocation="CenterScreen"
 9         Background="White"
10         Title="MainView" Height="650" Width="1300">
11     <Grid>
12         <Grid.RowDefinitions>
13             <RowDefinition Height="Auto" />
14             <RowDefinition />
15         </Grid.RowDefinitions>
16         <Grid.ColumnDefinitions>
17             <ColumnDefinition Width="200" />
18             <ColumnDefinition Width="300" />
19             <ColumnDefinition />           
20         </Grid.ColumnDefinitions>
21         <Button Content="执行" HorizontalAlignment="Stretch" Margin="3"  Command="{Binding ExecuteCommand}" />
22         <TextBlock Text="循序流程" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
23         <TextBlock Text="流程图控制" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
24         <ListView Grid.Row="1" Grid.Column="0">
25             <ListViewItem Content="AAA" Tag="ProcessA">
26                 <i:Interaction.Triggers>
27                     <i:EventTrigger EventName="PreviewMouseLeftButtonDown">
28                         <i:CallMethodAction MethodName="ListViewItem_MouseLeftButtonDown" TargetObject="{Binding}" />
29                     </i:EventTrigger>
30                 </i:Interaction.Triggers>
31             </ListViewItem>
32             <ListViewItem Content="BBB" Tag="ProcessB">
33                 <i:Interaction.Triggers>
34                     <i:EventTrigger EventName="PreviewMouseLeftButtonDown">
35                         <i:CallMethodAction MethodName="ListViewItem_MouseLeftButtonDown" TargetObject="{Binding}" />
36                     </i:EventTrigger>
37                 </i:Interaction.Triggers></ListViewItem>
38             <ListViewItem Content="CCC" Tag="ProcessC">
39                 <i:Interaction.Triggers>
40                     <i:EventTrigger EventName="PreviewMouseLeftButtonDown">
41                         <i:CallMethodAction MethodName="ListViewItem_MouseLeftButtonDown" TargetObject="{Binding}" />
42                     </i:EventTrigger>
43                 </i:Interaction.Triggers></ListViewItem>
44             <ListViewItem Content="DDD" Tag="ProcessD">
45                 <i:Interaction.Triggers>
46                     <i:EventTrigger EventName="PreviewMouseLeftButtonDown">
47                         <i:CallMethodAction MethodName="ListViewItem_MouseLeftButtonDown" TargetObject="{Binding}" />
48                     </i:EventTrigger>
49                 </i:Interaction.Triggers></ListViewItem>
50         </ListView>
51         <ListBox Grid.Row="1" Grid.Column="1" AllowDrop="True" ItemsSource="{Binding ProcessList}">
52             <i:Interaction.Triggers>
53                 <i:EventTrigger EventName="Drop">
54                     <i:CallMethodAction MethodName="ListBox_Drop" TargetObject="{Binding}" />
55                 </i:EventTrigger>
56             </i:Interaction.Triggers>
57             <ListBox.ItemTemplate>
58                 <DataTemplate>
59                     <TextBlock Text="{Binding Name}">
60                     </TextBlock>
61                 </DataTemplate>
62             </ListBox.ItemTemplate>
63         </ListBox>
64         <ItemsControl Grid.Row="1" Grid.Column="2" Background="Transparent" AllowDrop="true" ItemsSource="{Binding ProcessList}">
65             <i:Interaction.Triggers>
66                 <i:EventTrigger EventName="Drop">
67                     <i:CallMethodAction MethodName="ListBox_Drop" TargetObject="{Binding}" />
68                 </i:EventTrigger>
69             </i:Interaction.Triggers>
70             <ItemsControl.ItemsPanel>
71                 <ItemsPanelTemplate>
72                     <Canvas Background="AliceBlue" />
73                 </ItemsPanelTemplate>
74             </ItemsControl.ItemsPanel>
75             <ItemsControl.ItemContainerStyle>
76                 <Style TargetType="ContentPresenter">
77                     <Setter Property="Canvas.Left" Value="{Binding X}"/>
78                     <Setter Property="Canvas.Top" Value="{Binding Y}"/>
79                 </Style>
80             </ItemsControl.ItemContainerStyle>
81 
82             <ItemsControl.ItemTemplate>
83                 <DataTemplate>
84                     <Grid>
85                         <Border Width="120" Height="30" Background="White" CornerRadius="5" Name="border">
86                             <TextBlock Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
87                         </Border>
88                     </Grid>
89                 </DataTemplate>
90             </ItemsControl.ItemTemplate>
91         </ItemsControl>
92     </Grid>
93 </Window>

实现拖拽模块到流程图后产生位置变化并设置样式