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 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 cmd = p.GetValue(this, null) as DelegateCommand; if (cmd != null) cmd.RaiseCanExecuteChanged(); } } FieldInfo[] fi = this.GetType().GetFields(); foreach (FieldInfo p in fi) { InvokePropertyChanged(p.Name); if (p.FieldType == typeof(ICommand)) { DelegateCommand cmd = p.GetValue(this) as DelegateCommand; if (cmd != null) cmd.RaiseCanExecuteChanged(); } } } } public class BackendSCConfigViewModel : ViewModelBase { private List _scItems; public ObservableCollection ScItemList { get; set; } public ICommand SetScCommand { get; set; } public ICommand ReloadCommand { get; set; } public BackendSCConfigViewModel() { ScItemList = new ObservableCollection(); Init(); SetScCommand = new DelegateCommand(SetSc); ReloadCommand = new DelegateCommand(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) { 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.SetPoint = item.Value.ToString(); item.InvokePropertyChanged(nameof(item.Value)); item.InvokePropertyChanged(nameof(item.SetPoint)); } } public void Init(string module = null) { if (_scItems != null) return; _scItems = SC.GetItemList(); Load(null); } public void Load(string module) { if (_scItems == null) return; ScItemList.Clear(); int i = 0; foreach (var scItem in _scItems) { i++; if (!string.IsNullOrWhiteSpace(module) && scItem.Path.Split('.')[0] != module) continue; ScItemList.Add(new NotifiableSCConfigItem() { 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, Index = i, }); } } } /// /// SCConfigView.xaml 的交互逻辑 /// 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(); } List modules = new List(); foreach (var item in (this.DataContext as BackendSCConfigViewModel).ScItemList) { modules.Add(item.Path.Split('.')[0]); } modules = modules.Distinct().ToList(); int i = 0; modules.ForEach(module => { RadioButton radioButton = new RadioButton() { Content = module, Margin = new Thickness(10, 0, 0, 0), FontSize = 15 }; sp1.Children.Add(radioButton); radioButton.Checked += RadioButton_Checked; if (i == 0) { radioButton.IsChecked = true; i++; } }); } private void RadioButton_Checked(object sender, RoutedEventArgs e) { (this.DataContext as BackendSCConfigViewModel).Load(((sender as RadioButton).Content.ToString())); } } }