| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- using System;
- using System.ComponentModel;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media.Animation;
- using TeamAAS.FlowEngine.Execution;
- using TeamAAS.ViewModels;
- namespace TeamAAS.Views
- {
- public partial class ProductView : UserControl
- {
- private const double ExpandedWidth = 280;
- private const double CollapsedWidth = 56;
- private const double AnimationMs = 250;
- private ProductViewModel _vm;
- public ProductView()
- {
- InitializeComponent();
- DataContext = new ProductViewModel();
- DataContextChanged += OnDataContextChanged;
- Loaded += OnLoaded;
- Unloaded += OnUnloaded;
- }
- private void OnLoaded(object sender, RoutedEventArgs e)
- {
- HookViewModel(DataContext as ProductViewModel);
- // 运行/停止状态变化 → 刷新"运行中锁定"遮罩与角标
- FlowRunner.Instance.RunStateChanged += OnRunStateChanged;
- // 切回产品页时立即按当前运行状态同步一次,避免在别处启停流程后画布锁定状态滞后
- _vm?.RefreshRunState();
- }
- private void OnUnloaded(object sender, RoutedEventArgs e)
- {
- FlowRunner.Instance.RunStateChanged -= OnRunStateChanged;
- }
- private void OnRunStateChanged()
- {
- _vm?.RefreshRunState();
- }
- private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- HookViewModel(e.NewValue as ProductViewModel);
- }
- private void HookViewModel(ProductViewModel vm)
- {
- if (_vm != null)
- _vm.PropertyChanged -= OnVmPropertyChanged;
- _vm = vm;
- if (_vm != null)
- {
- _vm.PropertyChanged += OnVmPropertyChanged;
- ApplySidebarState(_vm.IsSidebarCollapsed, animate: false);
- }
- }
- private void OnVmPropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- if (e.PropertyName == nameof(ProductViewModel.IsSidebarCollapsed))
- {
- ApplySidebarState(_vm.IsSidebarCollapsed, animate: true);
- }
- }
- /// <summary>
- /// 产品列表右键菜单"属性":直接走 code-behind 事件(Context 弹窗在 Popup 独立可视树中,
- /// MenuItem 的 Command 绑定无法可靠命中 ViewModel,故不经过绑定)。
- /// 右键哪个产品就编辑哪个产品(PlacementTarget 的 DataContext = 产品名)。
- /// </summary>
- private void ProductProperties_Click(object sender, RoutedEventArgs e)
- {
- var vm = DataContext as ProductViewModel;
- if (vm == null) return;
- string productName = null;
- if (sender is MenuItem mi
- && mi.Parent is ContextMenu cm
- && cm.PlacementTarget is FrameworkElement target)
- {
- productName = target.DataContext as string;
- }
- if (string.IsNullOrEmpty(productName)) return;
- vm.ShowProductProperties(productName);
- }
- /// <summary>产品列表右键菜单“全局变量”:编辑该产品的 Product 作用域变量(弹窗)。</summary>
- private void ProductGlobalVars_Click(object sender, RoutedEventArgs e)
- {
- var vm = DataContext as ProductViewModel;
- if (vm == null) return;
- string productName = null;
- if (sender is MenuItem mi
- && mi.Parent is ContextMenu cm
- && cm.PlacementTarget is FrameworkElement target)
- {
- productName = target.DataContext as string;
- }
- if (string.IsNullOrEmpty(productName)) return;
- vm.ShowProductGlobalVars(productName);
- }
- private void ApplySidebarState(bool collapsed, bool animate)
- {
- double targetWidth = collapsed ? CollapsedWidth : ExpandedWidth;
- if (animate)
- {
- var widthAnim = new DoubleAnimation(targetWidth, TimeSpan.FromMilliseconds(AnimationMs))
- {
- EasingFunction = new CubicEase { EasingMode = EasingMode.EaseInOut }
- };
- SidebarPanel.BeginAnimation(FrameworkElement.WidthProperty, widthAnim);
- }
- else
- {
- SidebarPanel.Width = targetWidth;
- }
- if (collapsed)
- {
- if (animate)
- {
- var fadeOut = new DoubleAnimation(1.0, 0.0, TimeSpan.FromMilliseconds(120));
- fadeOut.Completed += (s, args) =>
- {
- SidebarExpanded.Visibility = Visibility.Collapsed;
- SidebarCollapsed.Visibility = Visibility.Visible;
- SidebarCollapsed.Opacity = 0;
- var fadeIn = new DoubleAnimation(0.0, 1.0, TimeSpan.FromMilliseconds(130));
- SidebarCollapsed.BeginAnimation(UIElement.OpacityProperty, fadeIn);
- };
- SidebarExpanded.BeginAnimation(UIElement.OpacityProperty, fadeOut);
- }
- else
- {
- SidebarExpanded.Visibility = Visibility.Collapsed;
- SidebarCollapsed.Visibility = Visibility.Visible;
- SidebarCollapsed.Opacity = 1.0;
- }
- }
- else
- {
- if (animate)
- {
- var fadeOut = new DoubleAnimation(1.0, 0.0, TimeSpan.FromMilliseconds(120));
- fadeOut.Completed += (s, args) =>
- {
- SidebarCollapsed.Visibility = Visibility.Collapsed;
- SidebarExpanded.Visibility = Visibility.Visible;
- SidebarExpanded.Opacity = 0;
- var fadeIn = new DoubleAnimation(0.0, 1.0, TimeSpan.FromMilliseconds(130));
- SidebarExpanded.BeginAnimation(UIElement.OpacityProperty, fadeIn);
- };
- SidebarCollapsed.BeginAnimation(UIElement.OpacityProperty, fadeOut);
- }
- else
- {
- SidebarCollapsed.Visibility = Visibility.Collapsed;
- SidebarExpanded.Visibility = Visibility.Visible;
- SidebarExpanded.Opacity = 1.0;
- }
- }
- }
- }
- }
|