139 lines
4.7 KiB
C#
139 lines
4.7 KiB
C#
using BOB.Models;
|
|
using Common.PubEvent;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using System.Xml;
|
|
|
|
namespace BOB.ViewModels
|
|
{
|
|
public class StepsManagerViewModel:BindableBase
|
|
{
|
|
#region 属性
|
|
private string _Title;
|
|
public string Title
|
|
{
|
|
get => _Title;
|
|
private set
|
|
{
|
|
SetProperty(ref _Title, value);
|
|
}
|
|
}
|
|
private StepModel _SelectedStep;
|
|
public StepModel SelectedStep
|
|
{
|
|
get => _SelectedStep;
|
|
set
|
|
{
|
|
if (SetProperty(ref _SelectedStep, value))
|
|
{
|
|
GlobalVariables.SelectedStep = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
private ProgramModel _program = new();
|
|
public ProgramModel Program
|
|
{
|
|
get => _program;
|
|
set => SetProperty(ref _program, value);
|
|
}
|
|
private bool _IsAdmin = new();
|
|
public bool IsAdmin
|
|
{
|
|
get => _IsAdmin;
|
|
set => SetProperty(ref _IsAdmin, value);
|
|
}
|
|
GlobalVariables GlobalVariables { get; set; }
|
|
private List<StepModel> tmpCopyList = new List<StepModel>();
|
|
private IEventAggregator eventAggregator;
|
|
|
|
#endregion
|
|
public ICommand EditStepCommand { get;set; }
|
|
public ICommand CopyStepCommand { get;set; }
|
|
public ICommand PasteStepCommand { get;set; }
|
|
public ICommand DeleteStepCommand { get;set; }
|
|
public StepsManagerViewModel(GlobalVariables _GlobalVariables,IEventAggregator eventAggregator)
|
|
{
|
|
eventAggregator = eventAggregator;
|
|
GlobalVariables = _GlobalVariables;
|
|
Program = GlobalVariables.Program;
|
|
IsAdmin = GlobalVariables.IsAdmin;
|
|
EditStepCommand = new DelegateCommand(EditStep);
|
|
CopyStepCommand = new DelegateCommand(CopyStep);
|
|
PasteStepCommand = new DelegateCommand(PasteStep);
|
|
DeleteStepCommand = new DelegateCommand(DeleteStep);
|
|
Program.StepCollection.CollectionChanged += StepCollection_CollectionChanged;
|
|
}
|
|
#region 委托命令
|
|
private void EditStep()
|
|
{
|
|
if (IsAdmin && SelectedStep != null)
|
|
{
|
|
var stepToEdit = SelectedStep;
|
|
}
|
|
}
|
|
private void CopyStep()
|
|
{
|
|
if (IsAdmin && SelectedStep != null)
|
|
{
|
|
tmpCopyList.Clear();
|
|
tmpCopyList.Add(SelectedStep);
|
|
}
|
|
}
|
|
private void PasteStep()
|
|
{
|
|
if (IsAdmin && tmpCopyList.Any())
|
|
{
|
|
int insertIndex = Program.StepCollection.IndexOf(SelectedStep) + 1;
|
|
foreach (var item in tmpCopyList)
|
|
{
|
|
Program.StepCollection.Insert(insertIndex, new StepModel(item) { ID = Guid.NewGuid() });
|
|
insertIndex++;
|
|
}
|
|
}
|
|
}
|
|
private void DeleteStep()
|
|
{
|
|
if (IsAdmin && SelectedStep != null)
|
|
{
|
|
var tmpList = new List<StepModel> { SelectedStep };
|
|
foreach (var item in tmpList)
|
|
{
|
|
Program.StepCollection.Remove(item);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 辅助方法
|
|
private void StepCollection_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
|
|
{
|
|
// Add/Move/Remove 都会触发,这里判断具体情形
|
|
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add ||
|
|
e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Move||
|
|
e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
|
|
{
|
|
// 如果需要等 UI 更新完再处理,可也用 Dispatcher 延迟一小段时间
|
|
Application.Current?.Dispatcher.BeginInvoke(new Action(() =>
|
|
{
|
|
// after drop: 重新编号或其他处理
|
|
for (int i = 0; i < Program.StepCollection.Count; i++)
|
|
Program.StepCollection[i].Index = i + 1;
|
|
|
|
// 发布 Prism 事件(如果需要)
|
|
// _eventAggregator.GetEvent<StepMovedEvent>().Publish(...);
|
|
}), System.Windows.Threading.DispatcherPriority.Background);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|