| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 | using System.Collections.Generic;using System.Collections.ObjectModel;using System.ComponentModel;using System.Linq;using System.Reflection;using System.Windows;using System.Windows.Controls;using System.Windows.Input;using Aitex.Core.RT.SCCore;using Aitex.Core.UI.MVVM;namespace MECF.Framework.RT.Core.Backend{    public class NotifiableSCConfigItem : SCConfigItem, INotifyPropertyChanged    {        public string SetPoint { get; set; }        public int Index { get; set; }        public event PropertyChangedEventHandler PropertyChanged;        public void InvokePropertyChanged(string propertyName)        {            if (PropertyChanged != null)            {                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));            }        }        public void InvokePropertyChanged()        {            PropertyInfo[] ps = this.GetType().GetProperties();            foreach (PropertyInfo p in ps)            {                InvokePropertyChanged(p.Name);                if (p.PropertyType == typeof(ICommand))                {                    DelegateCommand<string> cmd = p.GetValue(this, null) as DelegateCommand<string>;                    if (cmd != null)                        cmd.RaiseCanExecuteChanged();                }            }            FieldInfo[] fi = this.GetType().GetFields();            foreach (FieldInfo p in fi)            {                InvokePropertyChanged(p.Name);                if (p.FieldType == typeof(ICommand))                {                    DelegateCommand<string> cmd = p.GetValue(this) as DelegateCommand<string>;                    if (cmd != null)                        cmd.RaiseCanExecuteChanged();                }            }        }    }    public class BackendSCConfigViewModel : ViewModelBase    {        private List<SCConfigItem> _scItems;        private ObservableCollection<NotifiableSCConfigItem> _ScItemList;        public ObservableCollection<NotifiableSCConfigItem> ScItemList         {            get { return _ScItemList; }            set { _ScItemList = value;this.InvokePropertyChanged(nameof(ScItemList)); }         }        public string TextFilter { get; set; }        public ICommand SetScCommand { get; set; }        public ICommand ReloadCommand { get; set; }        public BackendSCConfigViewModel()        {            Init();            SetScCommand = new DelegateCommand<string>(SetSc);            ReloadCommand = new DelegateCommand<string>(Reload);        }        private void SetSc(string obj)        {            NotifiableSCConfigItem item = ScItemList.First(x => x.PathName == obj);            if (item.Type != SCConfigType.String.ToString() && string.IsNullOrEmpty(item.SetPoint))                return;            SC.SetItemValueFromString(obj, item.SetPoint == null ? "" : item.SetPoint);            item.BoolValue = SC.GetConfigItem(obj).BoolValue;            item.IntValue = SC.GetConfigItem(obj).IntValue;            item.StringValue = SC.GetConfigItem(obj).StringValue;            item.DoubleValue = SC.GetConfigItem(obj).DoubleValue;            item.InvokePropertyChanged(nameof(item.Value));        }        public void Reload(string obj)        {            Init(TextFilter);            foreach (var item in ScItemList)            {                item.BoolValue = SC.GetConfigItem(item.PathName).BoolValue;                item.IntValue = SC.GetConfigItem(item.PathName).IntValue;                item.StringValue = SC.GetConfigItem(item.PathName).StringValue;                item.DoubleValue = SC.GetConfigItem(item.PathName).DoubleValue;                item.InvokePropertyChanged(nameof(item.Value));            }        }        void Init(string strFilter="")        {            _scItems = SC.GetItemList();            ScItemList = new ObservableCollection<NotifiableSCConfigItem>();            int _index = 0;            foreach (var scItem in _scItems.Where(x => string.IsNullOrEmpty(strFilter) || x.PathName.ToLower().Contains(strFilter.ToLower())))            {                _index++;                ScItemList.Add(new NotifiableSCConfigItem()                {                    Index = _index,                    BoolValue = scItem.BoolValue,                    Default = scItem.Default,                    Description = scItem.Description,                    DoubleValue = scItem.DoubleValue,                    IntValue = scItem.IntValue,                    Max = scItem.Max,                    Min = scItem.Min,                    Name = scItem.Name,                    Type = scItem.Type,                    Tag = scItem.Tag,                    Parameter = scItem.Parameter,                    Path = scItem.Path,                    SetPoint = scItem.Value.ToString(),                    StringValue = scItem.StringValue,                });            }        }    }    /// <summary>    /// SCConfigView.xaml 的交互逻辑    /// </summary>    public partial class BackendSCConfigView : UserControl    {        public BackendSCConfigView()        {            InitializeComponent();            this.Loaded += SCConfigView_Loaded;            this.IsVisibleChanged += BackendSCConfigView_IsVisibleChanged;        }        private void BackendSCConfigView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)        {            if (this.DataContext == null)            {                DataContext = new BackendSCConfigViewModel();            }            (this.DataContext as BackendSCConfigViewModel).Reload(null);        }        private void SCConfigView_Loaded(object sender, RoutedEventArgs e)        {            if (this.DataContext == null)            {                DataContext = new BackendSCConfigViewModel();            }         }     }}
 |