Files
aistudio-wpf-diagram/AIStudio.Wpf.Flowchart/Controls/FlowchartEditor.xaml.cs

212 lines
6.9 KiB
C#
Raw Normal View History

2022-12-02 23:06:31 +08:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
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;
using AIStudio.Wpf.DiagramDesigner;
using AIStudio.Wpf.Flowchart.Models;
using AIStudio.Wpf.Flowchart.ViewModels;
namespace AIStudio.Wpf.Flowchart.Controls
{
/// <summary>
/// FlowchartEditor.xaml 的交互逻辑
/// </summary>
[TemplatePart(Name = PART_DiagramControl, Type = typeof(DiagramControl))]
public partial class FlowchartEditor : UserControl
{
public const string PART_DiagramControl = "PART_DiagramControl";
private DiagramControl _diagramControl;
private IDiagramServiceProvider _service
{
get
{
return DiagramServicesProvider.Instance.Provider;
}
}
private DiagramViewModel _diagramViewModel = new DiagramViewModel()
{
ShowGrid = true,
GridCellSize = new Size(100, 60),
GridMargin = 0d,
CellHorizontalAlignment = CellHorizontalAlignment.Center,
CellVerticalAlignment = CellVerticalAlignment.Center,
PageSizeType = PageSizeType.Custom,
PageSize = new Size(double.NaN, double.NaN),
};
static FlowchartEditor()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(FlowchartEditor), new FrameworkPropertyMetadata(typeof(FlowchartEditor)));
}
public FlowchartEditor()
{
_diagramViewModel.PropertyChanged += DiagramViewModel_PropertyChanged;
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_diagramControl = GetTemplateChild(PART_DiagramControl) as DiagramControl;
_diagramControl.HorizontalAlignment = HorizontalAlignment.Stretch;
_diagramControl.VerticalAlignment = VerticalAlignment.Stretch;
_diagramControl.DataContext = _diagramViewModel;
}
private void DiagramViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "IsSelected")
{
SelectedObject = _diagramViewModel.SelectedItems?.FirstOrDefault();
}
}
//一点要绑定不为空的FlowchartModel才能用即便为空的也要new一个再来绑定
public static readonly DependencyProperty FlowchartModelProperty =
DependencyProperty.Register(nameof(FlowchartModel),
typeof(FlowchartModel),
typeof(FlowchartEditor),
new FrameworkPropertyMetadata(null, OnFlowchartModelChanged));
public FlowchartModel FlowchartModel
{
get
{
return (FlowchartModel)GetValue(FlowchartModelProperty);
}
set
{
SetValue(FlowchartModelProperty, value);
}
}
private static void OnFlowchartModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var view = d as FlowchartEditor;
var model = e.NewValue as FlowchartModel;
if (model != null)
{
view.CreateFlowchartModel(model);
}
}
private void CreateFlowchartModel(FlowchartModel model)
{
_service.DrawModeViewModel.VectorLineDrawMode = DrawMode.BoundaryConnectingLine;
_diagramViewModel.Items.Clear();
if (model != null)
{
foreach (var node in model.Nodes)
{
_diagramViewModel.DirectAddItemCommand.Execute(node);
}
foreach (var link in model.Links)
{
_diagramViewModel.DirectAddItemCommand.Execute(link);
}
}
}
#region ToolBox
/// <summary>
/// 附加组件模板
/// </summary>
public static readonly DependencyProperty ToolBoxProperty = DependencyProperty.Register(
nameof(ToolBox), typeof(ControlTemplate), typeof(FlowchartEditor), new FrameworkPropertyMetadata(default(ControlTemplate)));
public ControlTemplate ToolBox
{
get
{
return (ControlTemplate)GetValue(ToolBoxProperty);
}
set
{
SetValue(ToolBoxProperty, value);
}
}
#endregion
#region ToolBox
/// <summary>
/// 附加组件模板
/// </summary>
public static readonly DependencyProperty PropertiesBoxProperty = DependencyProperty.Register(
nameof(PropertiesBox), typeof(ControlTemplate), typeof(FlowchartEditor), new FrameworkPropertyMetadata(default(ControlTemplate)));
public ControlTemplate PropertiesBox
{
get
{
return (ControlTemplate)GetValue(PropertiesBoxProperty);
}
set
{
SetValue(PropertiesBoxProperty, value);
}
}
#endregion
#region SelectedObject
public static readonly DependencyProperty SelectedObjectProperty = DependencyProperty.Register(nameof(SelectedObject), typeof(object), typeof(FlowchartEditor), new UIPropertyMetadata(default(object)));
public object SelectedObject
{
get
{
return (object)GetValue(SelectedObjectProperty);
}
set
{
SetValue(SelectedObjectProperty, value);
}
}
#endregion
#region Users
public static readonly DependencyProperty UsersProperty
= DependencyProperty.Register(nameof(Users), typeof(List<SelectOption>), typeof(FlowchartEditor), new UIPropertyMetadata(default(List<SelectOption>)));
public List<SelectOption> Users
{
get
{
return (List<SelectOption>)GetValue(UsersProperty);
}
set
{
SetValue(UsersProperty, value);
}
}
#endregion
#region Roles
public static readonly DependencyProperty RolesProperty
= DependencyProperty.Register(nameof(Roles), typeof(List<SelectOption>), typeof(FlowchartEditor), new UIPropertyMetadata(default(List<SelectOption>)));
public List<SelectOption> Roles
{
get
{
return (List<SelectOption>)GetValue(RolesProperty);
}
set
{
SetValue(RolesProperty, value);
}
}
#endregion
}
}