From 5ee0c1ce26b2dddf44c6700314ead9714c36ade3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=89=BE=E7=AB=B9?= Date: Sun, 29 Jan 2023 22:54:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Customization/CustomPortViewModel.cs | 39 +++ .../Views/Customization/CustomPortView.xaml | 29 ++ .../Customization/CustomPortView.xaml.cs | 26 ++ ...Wpf.DiagramDesigner_sezm4xxq_wpftmp.csproj | 267 ++++++++++++++++++ .../Controls/Connector.cs | 15 +- .../Controls/DesignerCanvas.cs | 16 +- .../Converters/NullOrEmptyToBoolConverter.cs | 33 +++ .../Themes/ConnectorItem.xaml | 70 ++--- .../Themes/DesignerItem.xaml | 4 +- .../UserControls/DiagramControl.xaml | 138 +++++---- .../UserControls/TextControl.xaml.cs | 2 +- .../Connector/ConnectorLabelModel.cs | 14 +- .../Connector/FullyCreatedConnectorInfo.cs | 3 +- .../SelectableDesignerItemViewModelBase.cs | 15 +- .../BaseViewModel/SelectableViewModelBase.cs | 2 +- 15 files changed, 548 insertions(+), 125 deletions(-) create mode 100644 AIStudio.Wpf.DiagramDesigner.Demo/ViewModels/Customization/CustomPortViewModel.cs create mode 100644 AIStudio.Wpf.DiagramDesigner.Demo/Views/Customization/CustomPortView.xaml create mode 100644 AIStudio.Wpf.DiagramDesigner.Demo/Views/Customization/CustomPortView.xaml.cs create mode 100644 AIStudio.Wpf.DiagramDesigner/AIStudio.Wpf.DiagramDesigner_sezm4xxq_wpftmp.csproj create mode 100644 AIStudio.Wpf.DiagramDesigner/Converters/NullOrEmptyToBoolConverter.cs diff --git a/AIStudio.Wpf.DiagramDesigner.Demo/ViewModels/Customization/CustomPortViewModel.cs b/AIStudio.Wpf.DiagramDesigner.Demo/ViewModels/Customization/CustomPortViewModel.cs new file mode 100644 index 0000000..2a82f43 --- /dev/null +++ b/AIStudio.Wpf.DiagramDesigner.Demo/ViewModels/Customization/CustomPortViewModel.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Windows; + +namespace AIStudio.Wpf.DiagramDesigner.Demo.ViewModels +{ + class CustomPortViewModel : BaseViewModel + { + public CustomPortViewModel() + { + Title = "Simple"; + Info = "A simple example of AIStudio.Wpf.DiagramDesigner."; + + DiagramViewModel = new DiagramViewModel(); + DiagramViewModel.PageSizeType = PageSizeType.Custom; + DiagramViewModel.PageSize = new Size(double.NaN, double.NaN); + DiagramViewModel.ColorViewModel = new ColorViewModel(); + DiagramViewModel.ColorViewModel.FillColor.Color = System.Windows.Media.Colors.Orange; + + DefaultDesignerItemViewModel node1 = new DefaultDesignerItemViewModel(DiagramViewModel) { Left = 50, Top = 50, Text = "1" }; + DiagramViewModel.DirectAddItemCommand.Execute(node1); + + DefaultDesignerItemViewModel node2 = new DefaultDesignerItemViewModel(DiagramViewModel) { Left = 300, Top = 300, Text = "2" }; + DiagramViewModel.DirectAddItemCommand.Execute(node2); + + DefaultDesignerItemViewModel node3 = new DefaultDesignerItemViewModel(DiagramViewModel) { Left = 300, Top = 50, Text = "3" }; + DiagramViewModel.DirectAddItemCommand.Execute(node3); + + ConnectionViewModel connector1 = new ConnectionViewModel(DiagramViewModel, node1.RightConnector, node2.LeftConnector, DrawMode.ConnectingLineSmooth, RouterMode.RouterNormal); + DiagramViewModel.DirectAddItemCommand.Execute(connector1); + + ConnectionViewModel connector2 = new ConnectionViewModel(DiagramViewModel, node2.RightConnector, node3.RightConnector, DrawMode.ConnectingLineStraight, RouterMode.RouterOrthogonal); + DiagramViewModel.DirectAddItemCommand.Execute(connector2); + + DiagramViewModel.ClearSelectedItemsCommand.Execute(null); + } + } +} diff --git a/AIStudio.Wpf.DiagramDesigner.Demo/Views/Customization/CustomPortView.xaml b/AIStudio.Wpf.DiagramDesigner.Demo/Views/Customization/CustomPortView.xaml new file mode 100644 index 0000000..2aa7070 --- /dev/null +++ b/AIStudio.Wpf.DiagramDesigner.Demo/Views/Customization/CustomPortView.xaml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/AIStudio.Wpf.DiagramDesigner.Demo/Views/Customization/CustomPortView.xaml.cs b/AIStudio.Wpf.DiagramDesigner.Demo/Views/Customization/CustomPortView.xaml.cs new file mode 100644 index 0000000..7b17c2d --- /dev/null +++ b/AIStudio.Wpf.DiagramDesigner.Demo/Views/Customization/CustomPortView.xaml.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace AIStudio.Wpf.DiagramDesigner.Demo.Views +{ + /// + /// CustomPortView.xaml 的交互逻辑 + /// + public partial class CustomPortView : UserControl + { + public CustomPortView() + { + InitializeComponent(); + } + } +} diff --git a/AIStudio.Wpf.DiagramDesigner/AIStudio.Wpf.DiagramDesigner_sezm4xxq_wpftmp.csproj b/AIStudio.Wpf.DiagramDesigner/AIStudio.Wpf.DiagramDesigner_sezm4xxq_wpftmp.csproj new file mode 100644 index 0000000..67a15f9 --- /dev/null +++ b/AIStudio.Wpf.DiagramDesigner/AIStudio.Wpf.DiagramDesigner_sezm4xxq_wpftmp.csproj @@ -0,0 +1,267 @@ + + + AIStudio.Wpf.DiagramDesigner + obj\Debug\ + obj\ + F:\aistudio.-wpf.-diagram\AIStudio.Wpf.DiagramDesigner\obj\ + <_TargetAssemblyProjectName>AIStudio.Wpf.DiagramDesigner + + + + true + AIStudio.Wpf.Controls + akwkevin + https://gitee.com/akwkevin + A.png + + + 1.0.4 + 一个Wpf的Diagram控件基础库 + + + + + + True + + + + + + + + + + + + + + + + + + + PreserveNewest + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AIStudio.Wpf.DiagramDesigner/Controls/Connector.cs b/AIStudio.Wpf.DiagramDesigner/Controls/Connector.cs index b82b2ce..58ac648 100644 --- a/AIStudio.Wpf.DiagramDesigner/Controls/Connector.cs +++ b/AIStudio.Wpf.DiagramDesigner/Controls/Connector.cs @@ -9,10 +9,8 @@ using System.Windows.Media; namespace AIStudio.Wpf.DiagramDesigner { - public class Connector : Control + public class Connector : ContentControl { - - protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); @@ -34,6 +32,17 @@ namespace AIStudio.Wpf.DiagramDesigner return element as DesignerCanvas; } + public FullyCreatedConnectorInfo Info + { + get + { + if (Content is FullyCreatedConnectorInfo connectorInfo) + return connectorInfo; + + return this.DataContext as FullyCreatedConnectorInfo; + } + } + } diff --git a/AIStudio.Wpf.DiagramDesigner/Controls/DesignerCanvas.cs b/AIStudio.Wpf.DiagramDesigner/Controls/DesignerCanvas.cs index 4e49b05..2a7230f 100644 --- a/AIStudio.Wpf.DiagramDesigner/Controls/DesignerCanvas.cs +++ b/AIStudio.Wpf.DiagramDesigner/Controls/DesignerCanvas.cs @@ -280,7 +280,8 @@ namespace AIStudio.Wpf.DiagramDesigner { sourceConnector = value; connectorsHit.Add(sourceConnector); - FullyCreatedConnectorInfo sourceDataItem = sourceConnector.DataContext as FullyCreatedConnectorInfo; + + FullyCreatedConnectorInfo sourceDataItem = sourceConnector.Info; Rect rectangleBounds = sourceConnector.TransformToVisual(this).TransformBounds(new Rect(sourceConnector.RenderSize)); @@ -348,10 +349,9 @@ namespace AIStudio.Wpf.DiagramDesigner // drag operation we cache the start point rubberbandSelectionStartPoint = e.GetPosition(this); - IDiagramViewModel vm = (this.DataContext as IDiagramViewModel); if (!(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))) { - vm.ClearSelectedItemsCommand.Execute(null); + _viewModel.ClearSelectedItemsCommand.Execute(null); } e.Handled = true; @@ -451,11 +451,11 @@ namespace AIStudio.Wpf.DiagramDesigner if (sourceConnector != null) { - FullyCreatedConnectorInfo sourceDataItem = sourceConnectorInfo ?? (sourceConnector.DataContext as FullyCreatedConnectorInfo); + FullyCreatedConnectorInfo sourceDataItem = sourceConnectorInfo ?? (sourceConnector.Info); if (connectorsHit.Count() == 2) { Connector sinkConnector = connectorsHit.Last(); - FullyCreatedConnectorInfo sinkDataItem = sinkConnector.DataContext as FullyCreatedConnectorInfo; + FullyCreatedConnectorInfo sinkDataItem = sinkConnector.Info; int indexOfLastTempConnection = sinkDataItem.DataItem.Root.Items.Count - 1; sinkDataItem.DataItem.Root.DirectRemoveItemCommand.Execute( @@ -562,7 +562,7 @@ namespace AIStudio.Wpf.DiagramDesigner DragObject dragObject = e.Data.GetData(typeof(DragObject)) as DragObject; if (dragObject != null) { - (DataContext as IDiagramViewModel).ClearSelectedItemsCommand.Execute(null); + _viewModel.ClearSelectedItemsCommand.Execute(null); Point position = e.GetPosition(this); DesignerItemViewModelBase itemBase = null; if (dragObject.DesignerItem != null) @@ -589,7 +589,7 @@ namespace AIStudio.Wpf.DiagramDesigner { foreach (var file in files) { - (DataContext as IDiagramViewModel).ClearSelectedItemsCommand.Execute(null); + _viewModel.ClearSelectedItemsCommand.Execute(null); Point position = e.GetPosition(this); ImageItemViewModel itemBase = new ImageItemViewModel(); itemBase.Icon = file; @@ -599,7 +599,7 @@ namespace AIStudio.Wpf.DiagramDesigner itemBase.Left = Math.Max(0, position.X - itemBase.ItemWidth / 2); itemBase.Top = Math.Max(0, position.Y - itemBase.ItemHeight / 2); - (DataContext as IDiagramViewModel).AddItemCommand.Execute(itemBase); + _viewModel.AddItemCommand.Execute(itemBase); } } e.Handled = true; diff --git a/AIStudio.Wpf.DiagramDesigner/Converters/NullOrEmptyToBoolConverter.cs b/AIStudio.Wpf.DiagramDesigner/Converters/NullOrEmptyToBoolConverter.cs new file mode 100644 index 0000000..c23d5fe --- /dev/null +++ b/AIStudio.Wpf.DiagramDesigner/Converters/NullOrEmptyToBoolConverter.cs @@ -0,0 +1,33 @@ +using System; +using System.Globalization; +using System.Windows; +using System.Windows.Data; + +namespace AIStudio.Wpf.DiagramDesigner +{ + public class NullOrEmptyToBoolConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + return string.IsNullOrEmpty(value?.ToString()); + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + return Binding.DoNothing; + } + } + + public class NotNullOrEmptyToBoolConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + return !string.IsNullOrEmpty(value?.ToString()); + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + return Binding.DoNothing; + } + } +} diff --git a/AIStudio.Wpf.DiagramDesigner/Themes/ConnectorItem.xaml b/AIStudio.Wpf.DiagramDesigner/Themes/ConnectorItem.xaml index 4b9a3b4..2359ff2 100644 --- a/AIStudio.Wpf.DiagramDesigner/Themes/ConnectorItem.xaml +++ b/AIStudio.Wpf.DiagramDesigner/Themes/ConnectorItem.xaml @@ -3,56 +3,28 @@ xmlns:s="clr-namespace:AIStudio.Wpf.DiagramDesigner" xmlns:c="clr-namespace:AIStudio.Wpf.DiagramDesigner.Controls"> - - - - - - - - - - - + + + + + + + + + + + + - - + @@ -538,7 +571,7 @@ - + @@ -547,14 +580,14 @@ - + - + @@ -583,13 +616,20 @@ + - + + + + + + + @@ -828,7 +868,7 @@ - +