ParameterEdit.xaml.cs 5.7 KB

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