ParameterEdit.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using Prism.Commands;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. using TeamAAS_VP.Models;
  20. namespace TeamAAS_VP.Controls
  21. {
  22. /// <summary>
  23. /// ParameterEdit.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class ParameterEdit : UserControl, INotifyPropertyChanged
  26. {
  27. public ParameterEdit()
  28. {
  29. InitializeComponent();
  30. DataContext = this;
  31. }
  32. private ProductGlobalParam _SelectParam;
  33. public ProductGlobalParam SelectParam
  34. {
  35. get { return _SelectParam; }
  36. set { SetProperty(ref _SelectParam,value); }
  37. }
  38. private ObservableCollection<ProductGlobalParam> _GlobalParamListCollection;
  39. /// <summary>
  40. /// 产品全局参数集合
  41. /// </summary>
  42. public ObservableCollection<ProductGlobalParam> GlobalParamListCollection
  43. {
  44. get { return _GlobalParamListCollection; }
  45. set { SetProperty(ref _GlobalParamListCollection, value); }
  46. }
  47. private DelegateCommand _AddCommand;
  48. public DelegateCommand AddCommand =>
  49. _AddCommand ?? (_AddCommand = new DelegateCommand(ExecuteAddCommand));
  50. private DelegateCommand<ProductGlobalParam> _RemoveCommand;
  51. public DelegateCommand<ProductGlobalParam> RemoveCommand =>
  52. _RemoveCommand ?? (_RemoveCommand = new DelegateCommand<ProductGlobalParam>(ExecuteRemoveCommand));
  53. void ExecuteRemoveCommand(ProductGlobalParam param)
  54. {
  55. GlobalParamListCollection.Remove(param);
  56. for (int i = 0; i < GlobalParamListCollection.Count; i++)
  57. {
  58. GlobalParamListCollection[i].Number = i;
  59. }
  60. }
  61. void ExecuteAddCommand()
  62. {
  63. GlobalParamListCollection.Add(new ProductGlobalParam() { Number= GlobalParamListCollection .Count});
  64. }
  65. #region 属性通知
  66. /// <summary>
  67. /// Occurs when a property value changes.
  68. /// </summary>
  69. public event PropertyChangedEventHandler PropertyChanged;
  70. /// <summary>
  71. /// Checks if a property already matches a desired value. Sets the property and
  72. /// notifies listeners only when necessary.
  73. /// </summary>
  74. /// <typeparam name="T">Type of the property.</typeparam>
  75. /// <param name="storage">Reference to a property with both getter and setter.</param>
  76. /// <param name="value">Desired value for the property.</param>
  77. /// <param name="propertyName">Name of the property used to notify listeners. This
  78. /// value is optional and can be provided automatically when invoked from compilers that
  79. /// support CallerMemberName.</param>
  80. /// <returns>True if the value was changed, false if the existing value matched the
  81. /// desired value.</returns>
  82. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  83. {
  84. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  85. storage = value;
  86. RaisePropertyChanged(propertyName);
  87. return true;
  88. }
  89. /// <summary>
  90. /// Checks if a property already matches a desired value. Sets the property and
  91. /// notifies listeners only when necessary.
  92. /// </summary>
  93. /// <typeparam name="T">Type of the property.</typeparam>
  94. /// <param name="storage">Reference to a property with both getter and setter.</param>
  95. /// <param name="value">Desired value for the property.</param>
  96. /// <param name="propertyName">Name of the property used to notify listeners. This
  97. /// value is optional and can be provided automatically when invoked from compilers that
  98. /// support CallerMemberName.</param>
  99. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  100. /// <returns>True if the value was changed, false if the existing value matched the
  101. /// desired value.</returns>
  102. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  103. {
  104. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  105. storage = value;
  106. onChanged?.Invoke();
  107. RaisePropertyChanged(propertyName);
  108. return true;
  109. }
  110. /// <summary>
  111. /// Raises this object's PropertyChanged event.
  112. /// </summary>
  113. /// <param name="propertyName">Name of the property used to notify listeners. This
  114. /// value is optional and can be provided automatically when invoked from compilers
  115. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  116. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  117. {
  118. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  119. }
  120. /// <summary>
  121. /// Raises this object's PropertyChanged event.
  122. /// </summary>
  123. /// <param name="args">The PropertyChangedEventArgs</param>
  124. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  125. {
  126. PropertyChanged?.Invoke(this, args);
  127. }
  128. #endregion
  129. }
  130. }