| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 | 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 构造函数        /// <summary>        /// 构造函数        /// </summary>        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        /// <summary>        /// TableDataSource        /// </summary>        public List<ConfigNode> TableDataSource        {            get { return _tableDataSource; }            set            {                _tableDataSource = value;                this.NotifyOfPropertyChange(nameof(TableDataSource));            }        }        private List<ConfigNode> _tableDataSource = new List<ConfigNode>();        #endregion        #region 属性 TableDataSource的显示视图        /// <summary>        /// TableDataSource的显示视图        /// </summary>        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        /// <summary>        /// TableBtnNodes        /// </summary>        public List<ConfigNode> TableBtnNodes        {            get { return _tableBtnNodes; }            set            {                _tableBtnNodes = value;                this.NotifyOfPropertyChange(nameof(TableBtnNodes));            }        }        private List<ConfigNode> _tableBtnNodes = new List<ConfigNode>();        #endregion        #region 属性 LeftTreeNodes        /// <summary>        /// LeftTreeNodes        /// </summary>        public List<ParamBase> LeftTreeNodes        {            get { return _leftTreeNodes; }            set            {                _leftTreeNodes = value;                this.NotifyOfPropertyChange(nameof(LeftTreeNodes));            }        }        private List<ParamBase> _leftTreeNodes = new List<ParamBase>();        #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<AITAuxData> StatusTableDataSource { get; set; }        public List<AITAuxData> ShowStatusTableDataSource { get; set; } = new List<AITAuxData>();        #endregion        #endregion        #region 方法 配置和状态切换        /// <summary>        /// Config/Status 按钮切换        /// </summary>        /// <param name="obj"></param>        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        /// <summary>        /// table切换        /// </summary>        /// <param name="obj"></param>        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<ConfigNode>(tabNode.SubNodes);                TableDataViewSource = new ListCollectionView(TableDataSource);                GetDataOfConfigItems();            }        }        #endregion        #region 方法 选择配置        /// <summary>        /// 选择配置        /// </summary>        /// <param name="obj"></param>        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<ConfigItem> colData = new List<ConfigItem>();            var windowManager = IoC.Get<IWindowManager>();            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 私有方法 数据初始化        /// <summary>        /// 数据初始化        /// </summary>        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;                    var isInstalledItem = commonNode.OfType<ConfigItem>().Where(p => p.Name == "IsInstalled").FirstOrDefault();                    if (isInstalledItem != null)                    {                        string key = String.Format("{0}{1}{2}", isInstalledItem.Path, ".", isInstalledItem.Name);                        isInstalledItem.CurrentValue = SystemConfigProvider.Instance.GetValueByName(key);                    }                    List<ConfigItem> node = new List<ConfigItem>();                    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<ConfigNode>(TableBtnNodes.FirstOrDefault().SubNodes);            TableDataViewSource = new ListCollectionView(TableDataSource);            GetDataOfConfigItems();        }        #endregion        #region 私有方法 获取配置数据        /// <summary>        /// 获取配置数据        /// </summary>        private void GetDataOfConfigItems()        {            if (TableDataSource == null)            {                return;            }            new Task(() =>            {                Dictionary<string, ConfigItem> keyValuePairs = new Dictionary<string, ConfigItem>();                List<string> getConfigs = new List<string>();                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 私有方法 根据名称获取节点        /// <summary>        /// 根据名称获取节点        /// </summary>        /// <param name="parentNode"></param>        /// <param name="strName"></param>        /// <returns></returns>        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 重写方法 初始化        /// <summary>        /// 初始化        /// </summary>        protected override void OnInitialize()        {            base.OnInitialize();            InitData();        }        #endregion        #region 方法 切换到第一行        /// <summary>        /// 切换到第一行        /// </summary>        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 方法 切换到上一行        /// <summary>        /// 切换到上一行        /// </summary>        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 方法 切换到下一行        /// <summary>        /// 切换到下一行        /// </summary>        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 方法 切换到最后一行        /// <summary>        /// 切换到最后一行        /// </summary>        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<string, object> 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<string, object> data)        {            base.InvokeBeforeUpdateProperty(data);        }    }    public class AutoScrollBehavior : Behavior<ListView>    {        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);//在这里使用一的方法                    });                }            }        }    }}
 |