using Prism.Mvvm; using System.Linq; using Prism.Events; using SHJX.Service.Common.Event; using System.Collections.Generic; using SHJX.Service.Model.EventArgs; using SHJX.Service.Control.Interface; using System.Collections.ObjectModel; using SHJX.Service.Common.Constants; using SHJX.Service.Model.ControlModules; namespace SHJX.Service.Main.ViewModels { public class OpLiquidPosViewModel : BindableBase { #region Properties public ObservableCollection CenterControls { get; set; } #endregion public OpLiquidPosViewModel(IEventAggregator ea, IMainService service) { var positions = service.OtherPositions; CenterControls = new ObservableCollection(); for (int i = 0; i < positions.Count; i++) { CenterControlInfo control = new() { Index = i, ControlName = positions[i].Name, TagNameText = positions[i].TagText, BackColor = ColorNames.MainColor }; CenterControls.Add(control); } #region 监听 ea.GetEvent().Subscribe(ChangeElement); #endregion } #region Event Methods public void ChangeElement(List args) { foreach (ElementArgs arg in args) { CenterControlInfo control = CenterControls.Where(item => item.ControlName.Equals(arg.Name)).FirstOrDefault(); if (control is null) { continue; } CenterControls[control.Index].CenterText = !arg.Enable ? string.IsNullOrWhiteSpace(arg.Current) ? string.Empty : arg.Current.Length < 2 ? arg.Current : arg.Current[1..] : string.Empty; CenterControls[control.Index].BackColor = !arg.Enable ? ColorNames.SentColor : ColorNames.MainColor; } } #endregion } }