播放地址:20241120-.NET9.0+WPF实战三类流程化业务逻辑控制-10_哔哩哔哩_bilibili
第16-19节 调整代码让拖拽到控制流程图里的模块可以再次拖拽移动
MainView.xaml文件
主要调整ItemsControl中的节点增加几个事件,这里注意 TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=DataContext}"需要详细绑定内容
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 <Border.Effect> 87 <DropShadowEffect Color="Gray" ShadowDepth="0" BlurRadius="5" Opacity="0.2" /> 88 </Border.Effect> 89 <i:Interaction.Triggers> 90 <i:EventTrigger EventName="MouseLeftButtonDown"> 91 <i:CallMethodAction MethodName="Node_MouseLeftButtonDown" TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=DataContext}" /> 92 </i:EventTrigger> 93 <i:EventTrigger EventName="MouseLeftButtonUp"> 94 <i:CallMethodAction MethodName="Node_MouseLeftButtonUp" TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=DataContext}" /> 95 </i:EventTrigger> 96 <i:EventTrigger EventName="MouseMove"> 97 <i:CallMethodAction MethodName="Node_MouseMove" TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=DataContext}" /> 98 </i:EventTrigger> 99 </i:Interaction.Triggers> 100 <TextBlock Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock> 101 </Border> 102 </Grid> 103 </DataTemplate> 104 </ItemsControl.ItemTemplate> 105 </ItemsControl> 106 </Grid> 107 </Window>
MainViewModel.cs文件增加绑定的事件
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-60; 20 instance.Y=point.Y-15; 21 ProcessList.Add(instance); 22 } 23 bool is_moving = false; 24 Point old_point = new Point(); 25 public void Node_MouseLeftButtonDown(object obj, MouseButtonEventArgs e) 26 { 27 is_moving = true; 28 old_point = e.GetPosition((IInputElement)obj); 29 Mouse.Capture((IInputElement)obj); 30 } 31 public void Node_MouseLeftButtonUp(object obj, MouseButtonEventArgs e) 32 { 33 is_moving = false; 34 Mouse.Capture(null); 35 } 36 public void Node_MouseMove(object sender, MouseEventArgs e) 37 { 38 if (is_moving) 39 { 40 var new_point = e.GetPosition((IInputElement)sender); 41 var node_model=(sender as FrameworkElement).DataContext as NodeModel; 42 node_model.X += new_point.X- old_point.X; 43 node_model.Y+= new_point.Y - old_point.Y; 44 } 45 } 46 47 [RelayCommand] 48 private void Execute() 49 { 50 foreach (var item in ProcessList) 51 { 52 item.Execute(); 53 } 54 } 55 }