using Aitex.Core.Common.DeviceData; using Aitex.Core.Util; using Caliburn.Micro; using Caliburn.Micro.Core; using FurnaceUI.Controls.Common; using FurnaceUI.Models; using MECF.Framework.Common.DataCenter; using MECF.Framework.UI.Client.CenterViews.Configs.SystemConfig; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Input; using System.Windows.Interactivity; namespace FurnaceUI.Views.Parameter { public class AUXIndexViewModel : FurnaceUIViewModelBase { private const string AUX_PATH = "PM1.RecipeEditParameter.AUX"; private const string TABLE = "Table"; #region 构造函数 /// /// 构造函数 /// public AUXIndexViewModel() { } #endregion #region 模型字段 #region 属性 IsFirstCheck public bool IsFirstCheck { get { return _isFirstCheck; } set { _isFirstCheck = value; this.NotifyOfPropertyChange(nameof(IsFirstCheck)); } } private bool _isFirstCheck = true; #endregion #region 属性 IsSelectConfig public bool IsSelectConfig { get { return _isSelectConfig; } set { _isSelectConfig = value; this.NotifyOfPropertyChange(nameof(IsSelectConfig)); } } private bool _isSelectConfig = true; #endregion #region 属性 IsSelectStatus public bool IsSelectStatus { get { return _isSelectStatus; } set { _isSelectStatus = value; this.NotifyOfPropertyChange(nameof(IsSelectStatus)); } } private bool _isSelectStatus = false; #endregion #region 属性 TableDataSource /// /// TableDataSource /// public List TableDataSource { get { return _tableDataSource; } set { _tableDataSource = value; this.NotifyOfPropertyChange(nameof(TableDataSource)); } } private List _tableDataSource = new List(); #endregion #region 属性 TableDataSource的显示视图 /// /// TableDataSource的显示视图 /// public ListCollectionView TableDataViewSource { get { return _tableDataViewSource; } set { _tableDataViewSource = value; NotifyOfPropertyChange("TableDataViewSource"); } } private ListCollectionView _tableDataViewSource; #endregion #region 属性 AUXNode private ConfigNode AUXNode { get; set; } #endregion #region 属性 TableBtnNodes /// /// TableBtnNodes /// public List TableBtnNodes { get { return _tableBtnNodes; } set { _tableBtnNodes = value; this.NotifyOfPropertyChange(nameof(TableBtnNodes)); } } private List _tableBtnNodes = new List(); #endregion #region 属性 LeftTreeNodes /// /// LeftTreeNodes /// public List LeftTreeNodes { get { return _leftTreeNodes; } set { _leftTreeNodes = value; this.NotifyOfPropertyChange(nameof(LeftTreeNodes)); } } private List _leftTreeNodes = new List(); #endregion private int _statusSelectedIndex; public int StatusSelectedIndex { get=> _statusSelectedIndex; set { _statusSelectedIndex = value; NotifyOfPropertyChange(nameof(StatusSelectedIndex)); } } private int _statusIndex; #region 订阅属性 StatusTableDataSource [Subscription("PM1.CurrentAuxData")] public List StatusTableDataSource { get; set; } public List ShowStatusTableDataSource { get; set; } = new List(); #endregion #endregion #region 方法 配置和状态切换 /// /// Config/Status 按钮切换 /// /// public void SwitchListView(object obj) { if ("status".Equals(obj.ToString())) { IsSelectStatus = true; IsSelectConfig = false; } if ("setting".Equals(obj.ToString())) { IsSelectConfig = true; IsSelectStatus = false; } RadioButton radio = new RadioButton(); radio.Content = "Table1"; radio.GroupName = obj.ToString(); TableBtnNodes.FirstOrDefault().IsSelected = true; SwitchTable(radio); } #endregion #region 方法 切换Table /// /// table切换 /// /// public void SwitchTable(object obj) { var rad = obj as RadioButton; var tableNameStr = rad.Content.ToString(); var groupName = rad.GroupName.ToString(); if (groupName.ToLower().Contains("status")) { // StatusTableDataSource = AllStatusListDataSource.Where(a => a.TableName.Equals(tableNameStr)).ToList(); } else { var tabNode = TableBtnNodes.Find(a => a.Name.Equals(tableNameStr)); TableDataSource = new List(tabNode.SubNodes); TableDataViewSource = new ListCollectionView(TableDataSource); GetDataOfConfigItems(); } } #endregion #region 方法 选择配置 /// /// 选择配置 /// /// public void SelectionChangedHandle(object obj, object selectedValue) { if (obj == null || selectedValue == null) { return; } ConfigNode node = (ConfigNode)obj; if (obj == null) { return; } if (selectedValue is ConfigNode selectedItem) { if (obj != selectedItem) { return; } } List colData = new List(); var windowManager = IoC.Get(); var colNum = node.Name; colData = TableDataSource.Where(a => a.Name.Equals(colNum)).FirstOrDefault().Items; AUXEditViewModel editViewModel = new AUXEditViewModel(); editViewModel.TableNodeItems = colData; (windowManager as WindowManager)?.ShowDialogWithTitle(editViewModel, null, "Edit AUX"); GetDataOfConfigItems(); } #endregion #region 私有方法 数据初始化 /// /// 数据初始化 /// private void InitData() { ConfigNode rootNode = SystemConfigProvider.Instance.GetConfig(true); AUXNode = FindNodeByName(rootNode, AUX_PATH); foreach (var item in AUXNode.SubNodes) { if (!item.Name.Contains(TABLE)) { continue; } foreach (var tableItem in item.SubNodes) { var commonNode = AUXNode.SubNodes.Find(a => a.Name.Equals(tableItem.Name)).Items; List node = new List(); node.AddRange(commonNode); node.AddRange(tableItem.Items); tableItem.Items = node; } } //按钮初始化 TableBtnNodes = AUXNode.SubNodes.Where(a => a.Name.Contains(TABLE)).ToList(); TableBtnNodes.FirstOrDefault().IsSelected = true; //表格数据初始化 TableDataSource = new List(TableBtnNodes.FirstOrDefault().SubNodes); TableDataViewSource = new ListCollectionView(TableDataSource); GetDataOfConfigItems(); } #endregion #region 私有方法 获取配置数据 /// /// 获取配置数据 /// private void GetDataOfConfigItems() { if (TableDataSource == null) { return; } new Task(() => { Dictionary keyValuePairs = new Dictionary(); List getConfigs = new List(); foreach (var item in TableDataSource) { foreach (var tableItem in item.Items) { var key = $"{tableItem.Path}.{tableItem.Name}"; getConfigs.Add(key); keyValuePairs.Add(key, tableItem); } } var configs = QueryDataClient.Instance.Service.PollConfig(getConfigs); foreach (var key in configs.Keys) { keyValuePairs[key].CurrentValue = configs[key].ToString(); } }).Start(); } #endregion #region 私有方法 根据名称获取节点 /// /// 根据名称获取节点 /// /// /// /// private ConfigNode FindNodeByName(ConfigNode parentNode, string strName) { string strCates = strName.Split('.')[0]; ConfigNode node = parentNode.SubNodes.Find((x) => x.Name == strCates); if (node == null) return parentNode; else return FindNodeByName(node, strName.Replace(strCates + ".", "")); } #endregion #region 重写方法 初始化 /// /// 初始化 /// protected override void OnInitialize() { base.OnInitialize(); InitData(); } #endregion #region 方法 切换到第一行 /// /// 切换到第一行 /// RelayCommand _moveCurrentToFirstCommand; public ICommand MoveCurrentToFirstCommand { get { if (_moveCurrentToFirstCommand == null) { _moveCurrentToFirstCommand = new RelayCommand(param => this.MoveCurrentToFirst(), param => this.CanMoveCurrentToFirst()); } return _moveCurrentToFirstCommand; } } private bool CanMoveCurrentToFirst() { return TableDataViewSource?.CurrentPosition > 0; } private void MoveCurrentToFirst() { if (TableDataViewSource?.CurrentPosition > 0) { TableDataViewSource?.MoveCurrentToFirst(); } } #endregion #region 方法 切换到上一行 /// /// 切换到上一行 /// RelayCommand _moveCurrentToPreviousCommand; public ICommand MoveCurrentToPrevioustCommand { get { if (_moveCurrentToPreviousCommand == null) { _moveCurrentToPreviousCommand = new RelayCommand(param => this.MoveCurrentToPrevious(), param => this.CanMoveCurrentToPrevious()); } return _moveCurrentToPreviousCommand; } } private bool CanMoveCurrentToPrevious() { return TableDataViewSource?.CurrentPosition > 0; } private void MoveCurrentToPrevious() { if (TableDataViewSource?.CurrentPosition > 0) { TableDataViewSource?.MoveCurrentToPrevious(); } } #endregion #region 方法 切换到下一行 /// /// 切换到下一行 /// RelayCommand _moveCurrentToNextCommand; public ICommand MoveCurrentToNextCommand { get { if (_moveCurrentToNextCommand == null) { _moveCurrentToNextCommand = new RelayCommand(param => this.MoveCurrentToNext(), param => this.CanMoveCurrentToNext()); } return _moveCurrentToNextCommand; } } private bool CanMoveCurrentToNext() { bool isEnable = false; if (TableDataViewSource != null) { isEnable = TableDataViewSource?.CurrentPosition < TableDataViewSource?.Count - 1; } return isEnable; } private void MoveCurrentToNext() { if (TableDataViewSource?.CurrentPosition < TableDataViewSource?.Count - 1) { TableDataViewSource?.MoveCurrentToNext(); } } #endregion #region 方法 切换到最后一行 /// /// 切换到最后一行 /// RelayCommand _moveCurrentToLastCommand; public ICommand MoveCurrentToLastCommand { get { if (_moveCurrentToLastCommand == null) { _moveCurrentToLastCommand = new RelayCommand(param => this.MoveCurrentToLast(), param => this.CanMoveCurrentToLast()); } return _moveCurrentToLastCommand; } } private bool CanMoveCurrentToLast() { bool isEnable = false; if (TableDataViewSource != null) { isEnable = TableDataViewSource?.CurrentPosition < TableDataViewSource?.Count - 1; } return isEnable; } public void MoveCurrentToLast() { if (TableDataViewSource?.CurrentPosition < TableDataViewSource?.Count - 1) { TableDataViewSource?.MoveCurrentToLast(); } } #endregion protected override void InvokeAfterUpdateProperty(Dictionary data) { base.InvokeAfterUpdateProperty(data); if (ShowStatusTableDataSource == null || ShowStatusTableDataSource.Count == 0) { StatusTableDataSource.ToList().ForEach(x => ShowStatusTableDataSource.Add(x)); } else { StatusTableDataSource.ToList().ForEach(x => { var showStatus = ShowStatusTableDataSource.FirstOrDefault(s => s.Index == x.Index); showStatus.Feedback = x.Feedback; showStatus.SetPoint = x.SetPoint; showStatus.WarningLowLimit = x.WarningLowLimit; showStatus.WarningHighLimit = x.WarningHighLimit; showStatus.AlarmHighLimit = x.AlarmHighLimit; showStatus.AlarmLowLimit = x.AlarmLowLimit; showStatus.InvokePropertyChanged(); }); } } protected override void InvokeBeforeUpdateProperty(Dictionary data) { base.InvokeBeforeUpdateProperty(data); } } public class AutoScrollBehavior : Behavior { protected override void OnAttached() { AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged; base.OnAttached(); } protected override void OnDetaching() { AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged; base.OnDetaching(); } private void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (sender is ListView listView) { if (listView.SelectedItem != null) { listView.Dispatcher.BeginInvoke((System.Action)delegate { listView.UpdateLayout(); listView.ScrollIntoView(listView.SelectedItem);//在这里使用一的方法 }); } } } } }