This commit is contained in:
kwai
2023-06-16 19:03:15 +08:00
parent 1dd09a2240
commit 56c3aac8e8
12 changed files with 167 additions and 102 deletions

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
@@ -10,15 +12,15 @@ using AIStudio.Wpf.DiagramDesigner.Helpers;
namespace AIStudio.Wpf.DiagramDesigner
{
public class ItemsContainer : ContentControl
public class BlockItemsContainer : ContentControl
{
static ItemsContainer()
static BlockItemsContainer()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ItemsContainer), new FrameworkPropertyMetadata(typeof(ItemsContainer)));
DefaultStyleKeyProperty.OverrideMetadata(typeof(BlockItemsContainer), new FrameworkPropertyMetadata(typeof(BlockItemsContainer)));
}
public static readonly DependencyProperty BorderProperty = DependencyProperty.Register(
nameof(Border), typeof(object), typeof(ItemsContainer), new PropertyMetadata(default(object)));
nameof(Border), typeof(object), typeof(BlockItemsContainer), new PropertyMetadata(default(object)));
public object Border
{
@@ -27,7 +29,7 @@ namespace AIStudio.Wpf.DiagramDesigner
}
public static readonly DependencyProperty BorderTemplateProperty = DependencyProperty.Register(
nameof(BorderTemplate), typeof(DataTemplate), typeof(ItemsContainer), new PropertyMetadata(default(DataTemplate)));
nameof(BorderTemplate), typeof(DataTemplate), typeof(BlockItemsContainer), new PropertyMetadata(default(DataTemplate)));
[Bindable(true), Category("Content")]
public DataTemplate BorderTemplate
@@ -37,7 +39,7 @@ namespace AIStudio.Wpf.DiagramDesigner
}
public static readonly DependencyProperty BorderTemplateSelectorProperty = DependencyProperty.Register(
nameof(BorderTemplateSelector), typeof(DataTemplateSelector), typeof(ItemsContainer), new PropertyMetadata(default(DataTemplateSelector)));
nameof(BorderTemplateSelector), typeof(DataTemplateSelector), typeof(BlockItemsContainer), new PropertyMetadata(default(DataTemplateSelector)));
[Bindable(true), Category("Content")]
public DataTemplateSelector BorderTemplateSelector
@@ -47,7 +49,7 @@ namespace AIStudio.Wpf.DiagramDesigner
}
public static readonly DependencyProperty BorderStringFormatProperty = DependencyProperty.Register(
nameof(BorderStringFormat), typeof(string), typeof(ItemsContainer), new PropertyMetadata(default(string)));
nameof(BorderStringFormat), typeof(string), typeof(BlockItemsContainer), new PropertyMetadata(default(string)));
[Bindable(true), Category("Content")]
public string BorderStringFormat
@@ -58,7 +60,7 @@ namespace AIStudio.Wpf.DiagramDesigner
public static readonly DependencyProperty ParentPanelProperty =
DependencyProperty.Register(nameof(ParentPanel), typeof(FrameworkElement), typeof(ItemsContainer),
DependencyProperty.Register(nameof(ParentPanel), typeof(FrameworkElement), typeof(BlockItemsContainer),
new FrameworkPropertyMetadata(null));
public FrameworkElement ParentPanel
@@ -76,7 +78,7 @@ namespace AIStudio.Wpf.DiagramDesigner
public static readonly DependencyProperty GetOffSetFuncProperty =
DependencyProperty.Register(nameof(GetOffSetFunc),
typeof(Func<Point>),
typeof(ItemsContainer),
typeof(BlockItemsContainer),
new FrameworkPropertyMetadata(null));
public Func<Point> GetOffSetFunc
{
@@ -98,14 +100,14 @@ namespace AIStudio.Wpf.DiagramDesigner
}
}
public ItemsContainerInfo Info
public BlockItemsContainerInfo Info
{
get
{
if (Border is ItemsContainerInfo itemsContainerInfo)
if (Border is BlockItemsContainerInfo itemsContainerInfo)
return itemsContainerInfo;
return this.DataContext as ItemsContainerInfo;
return this.DataContext as BlockItemsContainerInfo;
}
}
@@ -119,11 +121,18 @@ namespace AIStudio.Wpf.DiagramDesigner
get; private set;
}
public ItemsContainer()
public Point? FirstPoint
{
get; private set;
}
public BlockItemsContainer()
{
}
public static List<BlockItemsContainer> Containers = new List<BlockItemsContainer>();
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
@@ -131,33 +140,31 @@ namespace AIStudio.Wpf.DiagramDesigner
GetOffSetFunc = GetOffSet;
}
Point? firstPoint;
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
firstPoint = e.GetPosition(this);
FirstPoint = e.GetPosition(this);
Containers.Add(this);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (firstPoint != null)
if (FirstPoint != null)
{
var currentPoint = e.GetPosition(this);
if (Math.Sqrt(Math.Pow(firstPoint.Value.X - currentPoint.X, 2) + Math.Pow(firstPoint.Value.Y - currentPoint.Y, 2)) > 5)
if (Math.Sqrt(Math.Pow(FirstPoint.Value.X - currentPoint.X, 2) + Math.Pow(FirstPoint.Value.Y - currentPoint.Y, 2)) > 5)
{
firstPoint = null;
FirstPoint = null;
if (Info?.Children?.Count > 0)
{
var borders = VisualHelper.FindVisualChildren<BlockBorder>(this);
BlockBorder innerblock = null;
BlockDesignerItemViewModel dragObject = null;
Point dragOffset = new Point();
foreach (var border in borders)
{
var itemsContainer = VisualHelper.TryFindParent<ItemsContainer>(border);
if (this != itemsContainer)
var itemsContainers = VisualHelper.TryFindParent<BlockItemsContainer>(border);
if (this != itemsContainers)
{
continue;
}
@@ -174,9 +181,10 @@ namespace AIStudio.Wpf.DiagramDesigner
if (DragObject != null)
{
Containers.ForEach(p => p.FirstPoint = null);
DesignerCanvas canvas = GetDesignerCanvas(this);
if (canvas != null)
{
{
canvas.SourceItemsContainer = this;
e.Handled = true;
}
@@ -189,7 +197,9 @@ namespace AIStudio.Wpf.DiagramDesigner
{
base.OnMouseUp(e);
firstPoint = null;
FirstPoint = null;
Containers.Clear();
}
public ConnectorOrientation Orientation

View File

@@ -0,0 +1,23 @@
<ItemsControl x:Class="AIStudio.Wpf.DiagramDesigner.BlockItemsControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:AIStudio.Wpf.DiagramDesigner"
xmlns:dd="clr-namespace:AIStudio.Wpf.DiagramDesigner"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<dd:BlockBorder>
<ContentControl Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
</dd:BlockBorder>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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
{
/// <summary>
/// BlockItemsControl.xaml 的交互逻辑
/// </summary>
public partial class BlockItemsControl : ItemsControl
{
public BlockItemsControl()
{
InitializeComponent();
}
protected override System.Windows.DependencyObject GetContainerForItemOverride()
{
return new ListBoxItem();
}
}
}

View File

@@ -84,8 +84,8 @@ namespace AIStudio.Wpf.DiagramDesigner
}
}
private ItemsContainer _sourceItemsContainer;
public ItemsContainer SourceItemsContainer
private BlockItemsContainer _sourceItemsContainer;
public BlockItemsContainer SourceItemsContainer
{
get
{
@@ -98,7 +98,7 @@ namespace AIStudio.Wpf.DiagramDesigner
_sourceItemsContainer = value;
if (_sourceItemsContainer != null)
{
ItemsContainerInfo sourceDataItem = _sourceItemsContainer.Info;
BlockItemsContainerInfo sourceDataItem = _sourceItemsContainer.Info;
sourceDataItem.DataItem.RemoveChild(_sourceItemsContainer.DragObject);