using Prism.Mvvm;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UIShare.UIViewModel;
namespace UIShare.GlobalVariable
{
///
/// 单个 Tab(主程序 / 错误程序)的子程序导航状态。
/// 主程序和错误程序各自持有一个独立实例,互不干扰。
///
public class ProgramNavigationState : BindableBase
{
/// 是否为错误程序 Tab
public bool IsErrorTab { get; set; }
public Stack NavigationStack { get; } = new();
private ProgramVM _currentProgram;
public ProgramVM CurrentProgram
{
get => _currentProgram;
set
{
if (SetProperty(ref _currentProgram, value))
RaisePropertyChanged(nameof(DisplaySteps));
}
}
///
/// DataGrid 实际绑定的步骤集合。
/// 根程序 + 错误 Tab → ErrorStepCollection;其他情况 → StepCollection。
///
public ObservableCollection DisplaySteps
{
get
{
if (CurrentProgram == null)
return new ObservableCollection();
// 在根程序且是错误 Tab → 显示错误步骤集合
if (NavigationStack.Count == 0 && IsErrorTab)
return CurrentProgram.ErrorStepCollection;
// 其他情况(主 Tab 或已进入子程序)→ 显示正常步骤集合
return CurrentProgram.StepCollection;
}
}
private string _breadcrumbPath = "主程序";
public string BreadcrumbPath
{
get => _breadcrumbPath;
set => SetProperty(ref _breadcrumbPath, value);
}
private bool _canGoBack;
public bool CanGoBack
{
get => _canGoBack;
set => SetProperty(ref _canGoBack, value);
}
///
/// 用根程序初始化导航状态
///
public void Initialize(ProgramVM rootProgram, string label)
{
NavigationStack.Clear();
CurrentProgram = rootProgram;
BreadcrumbPath = label;
CanGoBack = false;
}
///
/// 清空导航栈,回到根程序
///
public void Reset(ProgramVM rootProgram, string label)
{
NavigationStack.Clear();
CurrentProgram = rootProgram;
BreadcrumbPath = label;
CanGoBack = false;
}
///
/// 根据栈状态刷新 CanGoBack
///
public void UpdateCanGoBack() => CanGoBack = NavigationStack.Count > 0;
}
}