mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-04-07 16:36:35 +08:00
完成mvvm模式下,画布、节点编辑的基本重构
This commit is contained in:
@@ -5,8 +5,11 @@
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:Serein.Workbench.Views"
|
||||
xmlns:tool="clr-namespace:Serein.Workbench.Tool.Converters"
|
||||
xmlns:vm="clr-namespace:Serein.Workbench.ViewModels"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450" d:DesignWidth="800">
|
||||
d:DesignHeight="450"
|
||||
d:DesignWidth="800"
|
||||
d:DataContext="{d:DesignInstance vm:FlowCanvasViewModel}">
|
||||
|
||||
<UserControl.Resources>
|
||||
<tool:RightThumbPositionConverter x:Key="RightThumbPositionConverter" />
|
||||
@@ -16,22 +19,23 @@
|
||||
</UserControl.Resources>
|
||||
|
||||
<DockPanel x:Name="FlowChartStackPanel"
|
||||
ClipToBounds="True">
|
||||
ClipToBounds="True">
|
||||
|
||||
<Canvas ClipToBounds="True"
|
||||
x:Name="FlowChartCanvas"
|
||||
Background="#E1FBEA"
|
||||
AllowDrop="True"
|
||||
Width="1000"
|
||||
Height="600"
|
||||
MouseLeftButtonDown ="FlowChartCanvas_MouseLeftButtonDown"
|
||||
MouseLeftButtonUp="FlowChartCanvas_MouseLeftButtonUp"
|
||||
MouseDown="FlowChartCanvas_MouseDown"
|
||||
MouseUp="FlowChartCanvas_MouseUp"
|
||||
MouseMove="FlowChartCanvas_MouseMove"
|
||||
MouseWheel="FlowChartCanvas_MouseWheel"
|
||||
Drop="FlowChartCanvas_Drop"
|
||||
DragOver="FlowChartCanvas_DragOver"
|
||||
>
|
||||
x:Name="FlowChartCanvas"
|
||||
Background="#E1FBEA"
|
||||
AllowDrop="True"
|
||||
Width="{Binding Model.Width, Mode=TwoWay}"
|
||||
Height="{Binding Model.Height, Mode=TwoWay}"
|
||||
MouseLeftButtonDown ="FlowChartCanvas_MouseLeftButtonDown"
|
||||
MouseLeftButtonUp="FlowChartCanvas_MouseLeftButtonUp"
|
||||
MouseDown="FlowChartCanvas_MouseDown"
|
||||
MouseUp="FlowChartCanvas_MouseUp"
|
||||
MouseMove="FlowChartCanvas_MouseMove"
|
||||
MouseWheel="FlowChartCanvas_MouseWheel"
|
||||
Drop="FlowChartCanvas_Drop"
|
||||
DragOver="FlowChartCanvas_DragOver"
|
||||
>
|
||||
|
||||
<Rectangle x:Name="SelectionRectangle"
|
||||
Stroke="Blue"
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -19,29 +19,30 @@
|
||||
<!--DragOver="TabControl_DragOver"
|
||||
Drop="TabControl_Drop"
|
||||
AllowDrop="True"-->
|
||||
<TabControl SelectedItem="{Binding SelectedTab}" >
|
||||
<TabControl SelectedItem="{Binding SelectedTab}"
|
||||
SelectionChanged="TabControl_SelectionChanged"
|
||||
x:Name="CanvasTab">
|
||||
<TabControl.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<StackPanel>
|
||||
<StackPanel >
|
||||
<!-- 双击选项卡名称来进入编辑模式 -->
|
||||
<TextBlock Text="{Binding Name}"
|
||||
FontSize="18"
|
||||
Visibility="{Binding IsEditing, Converter={StaticResource InvertableBooleanToVisibilityConverter},ConverterParameter=Inverted}"
|
||||
Visibility="{Binding IsEditing, Converter={StaticResource InvertableBooleanToVisibilityConverter},ConverterParameter=Inverted}"
|
||||
PreviewMouseLeftButtonDown="TextBlock_PreviewMouseLeftButtonDown"
|
||||
MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"/>
|
||||
<!-- 编辑模式下显示TextBox -->
|
||||
<TextBox Text="{Binding Name, Mode=TwoWay}"
|
||||
FontSize="18"
|
||||
Visibility="{Binding IsEditing, Converter={StaticResource InvertableBooleanToVisibilityConverter},ConverterParameter=Normal}"
|
||||
KeyDown="TextBox_KeyDown"
|
||||
LostFocus="TextBox_LostFocus"/>
|
||||
KeyDown="TextBox_KeyDown" />
|
||||
</StackPanel>
|
||||
</DataTemplate>
|
||||
</TabControl.ItemTemplate>
|
||||
|
||||
<!-- Tabs Collection -->
|
||||
<TabControl.ItemsSource>
|
||||
<Binding Path="Tabs" />
|
||||
<Binding Path="CanvasTabs" />
|
||||
</TabControl.ItemsSource>
|
||||
|
||||
<!-- Content of the Tab (e.g., FlowCanvasView) -->
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Serein.Workbench.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -30,16 +31,18 @@ namespace Serein.Workbench.Views
|
||||
/// </summary>
|
||||
public partial class FlowEditView : UserControl
|
||||
{
|
||||
|
||||
public FlowEditView()
|
||||
{
|
||||
this.DataContext = App.GetService<Locator>().FlowEditViewModel;
|
||||
InitializeComponent();
|
||||
|
||||
}
|
||||
|
||||
private void TextBlock_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
var textBlock = sender as TextBlock;
|
||||
var tab = textBlock?.DataContext as FlowCanvasModel;
|
||||
var tab = textBlock?.DataContext as FlowCanvasViewModel;
|
||||
if (tab != null)
|
||||
{
|
||||
DragDrop.DoDragDrop(textBlock, tab, DragDropEffects.Move);
|
||||
@@ -47,7 +50,7 @@ namespace Serein.Workbench.Views
|
||||
}
|
||||
private void TabControl_DragOver(object sender, DragEventArgs e)
|
||||
{
|
||||
if (e.Data.GetDataPresent(typeof(FlowCanvasModel)))
|
||||
if (e.Data.GetDataPresent(typeof(FlowCanvasViewModel)))
|
||||
{
|
||||
e.Effects = DragDropEffects.Move;
|
||||
}
|
||||
@@ -59,73 +62,83 @@ namespace Serein.Workbench.Views
|
||||
|
||||
private void TabControl_Drop(object sender, DragEventArgs e)
|
||||
{
|
||||
var sourceTab = e.Data.GetData(typeof(FlowCanvasModel)) as FlowCanvasModel;
|
||||
var targetTab = (sender as TabControl)?.SelectedItem as FlowCanvasModel;
|
||||
var sourceTab = e.Data.GetData(typeof(FlowEditorTabModel)) as FlowEditorTabModel;
|
||||
var targetTab = (sender as TabControl)?.SelectedItem as FlowEditorTabModel;
|
||||
var viewModel = (FlowEditViewModel)this.DataContext;
|
||||
if (sourceTab != null && targetTab != null && sourceTab != targetTab)
|
||||
{
|
||||
var sourceIndex = viewModel.Tabs.IndexOf(sourceTab);
|
||||
var targetIndex = viewModel.Tabs.IndexOf(targetTab);
|
||||
var sourceIndex = viewModel.CanvasTabs.IndexOf(sourceTab);
|
||||
var targetIndex = viewModel.CanvasTabs.IndexOf(targetTab);
|
||||
|
||||
// 删除源项并插入到目标位置
|
||||
viewModel.Tabs.Remove(sourceTab);
|
||||
viewModel.Tabs.Insert(targetIndex, sourceTab);
|
||||
|
||||
viewModel.CanvasTabs.Remove(sourceTab);
|
||||
viewModel.CanvasTabs.Insert(targetIndex, sourceTab);
|
||||
|
||||
// 更新视图模型中的选中的Tab
|
||||
viewModel.SelectedTab = sourceTab;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 按下确认或回车
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
|
||||
{
|
||||
|
||||
if (e.Key == Key.Enter || e.Key == Key.Escape)
|
||||
{
|
||||
var textBox = sender as TextBox;
|
||||
var newName = textBox?.Text;
|
||||
if (string.IsNullOrEmpty(newName))
|
||||
if (sender is TextBox textBox
|
||||
&& textBox.DataContext is FlowEditorTabModel tab
|
||||
&& DataContext is FlowEditViewModel viewModel)
|
||||
{
|
||||
viewModel.EndEditingTab(tab, textBox.Text); // 确认新名称
|
||||
return;
|
||||
}
|
||||
var tab = textBox?.DataContext as FlowCanvasModel;
|
||||
if (tab != null)
|
||||
{
|
||||
var viewModel = (FlowEditViewModel)this.DataContext;
|
||||
viewModel.EndEditingTab(tab, newName); // 确认新名称
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var textBox = sender as TextBox;
|
||||
var newName = textBox?.Text;
|
||||
if (string.IsNullOrEmpty(newName))
|
||||
{
|
||||
return;
|
||||
}
|
||||
var tab = textBox?.DataContext as FlowCanvasModel;
|
||||
if (tab != null && tab.IsEditing)
|
||||
{
|
||||
var viewModel = (FlowEditViewModel)this.DataContext;
|
||||
viewModel.EndEditingTab(tab, newName); // 确认新名称
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 双击tab进入编辑状态
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (e.ClickCount == 2)
|
||||
{
|
||||
var textBlock = sender as TextBlock;
|
||||
var tab = textBlock?.DataContext as FlowCanvasModel;
|
||||
if (tab != null)
|
||||
if (sender is TextBlock textBlock
|
||||
&& textBlock.DataContext is FlowEditorTabModel tab
|
||||
&& DataContext is FlowEditViewModel viewModel)
|
||||
{
|
||||
var viewModel = (FlowEditViewModel)this.DataContext;
|
||||
viewModel.StartEditingTab(tab);
|
||||
viewModel.StartEditingTab(tab); // 确认新名称
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private FlowEditorTabModel lastTab;
|
||||
|
||||
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (sender is TabControl tabControl
|
||||
&& tabControl.SelectedIndex > 0
|
||||
&& DataContext is FlowEditViewModel viewModel
|
||||
&& viewModel.CanvasTabs[tabControl.SelectedIndex] is FlowEditorTabModel tab)
|
||||
{
|
||||
|
||||
viewModel.EndEditingTab(lastTab); // 确认新名称
|
||||
lastTab = tab;
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,15 @@
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:Serein.Workbench.Views"
|
||||
xmlns:vm="clr-namespace:Serein.Workbench.ViewModels"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="70" d:DesignWidth="350">
|
||||
d:DesignHeight="70" d:DesignWidth="350"
|
||||
d:DataContext="{d:DesignInstance vm:MainMenuBarViewModel}"
|
||||
>
|
||||
<Grid >
|
||||
<Menu DockPanel.Dock="Top" Grid.Row="0" Grid.ColumnSpan="5" Height="20">
|
||||
<MenuItem Header="项目">
|
||||
<MenuItem Header="保存项目" ></MenuItem>
|
||||
<MenuItem Header="保存项目" Command="{Binding SaveProjectCommand}"></MenuItem>
|
||||
<MenuItem Header="加载本地项目" ></MenuItem>
|
||||
<MenuItem Header="加载远程项目"></MenuItem>
|
||||
</MenuItem>
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace Serein.Workbench.Views
|
||||
{
|
||||
public MainMenuBarView()
|
||||
{
|
||||
this.DataContext = App.GetService<Locator>().MainViewModel;
|
||||
this.DataContext = App.GetService<Locator>().MainMenuBarViewModel;
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user