ParameterEdit.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. }
  32. private ProductGlobalParam _SelectParam;
  33. public ProductGlobalParam SelectParam
  34. {
  35. get { return _SelectParam; }
  36. set { SetProperty(ref _SelectParam, value); }
  37. }
  38. /// <summary>
  39. /// 产品全局参数集合 依赖属性(支持外部 Binding)
  40. /// </summary>
  41. public static readonly DependencyProperty GlobalParamListCollectionProperty =
  42. DependencyProperty.Register(
  43. nameof(GlobalParamListCollection),
  44. typeof(ObservableCollection<ProductGlobalParam>),
  45. typeof(ParameterEdit),
  46. new PropertyMetadata(null));
  47. /// <summary>
  48. /// 公开可绑定属性
  49. /// </summary>
  50. public ObservableCollection<ProductGlobalParam> GlobalParamListCollection
  51. {
  52. get => (ObservableCollection<ProductGlobalParam>)GetValue(GlobalParamListCollectionProperty);
  53. set => SetValue(GlobalParamListCollectionProperty, value);
  54. }
  55. private DelegateCommand _AddCommand;
  56. public DelegateCommand AddCommand =>
  57. _AddCommand ?? (_AddCommand = new DelegateCommand(ExecuteAddCommand));
  58. private DelegateCommand<ProductGlobalParam> _RemoveCommand;
  59. public DelegateCommand<ProductGlobalParam> RemoveCommand =>
  60. _RemoveCommand ?? (_RemoveCommand = new DelegateCommand<ProductGlobalParam>(ExecuteRemoveCommand));
  61. void ExecuteRemoveCommand(ProductGlobalParam param)
  62. {
  63. if (param == null || GlobalParamListCollection == null) return;
  64. GlobalParamListCollection.Remove(param);
  65. for (int i = 0; i < GlobalParamListCollection.Count; i++)
  66. {
  67. GlobalParamListCollection[i].Number = i;
  68. }
  69. }
  70. void ExecuteAddCommand()
  71. {
  72. if (GlobalParamListCollection == null)
  73. {
  74. GlobalParamListCollection = new ObservableCollection<ProductGlobalParam>();
  75. }
  76. GlobalParamListCollection.Add(new ProductGlobalParam() { Number= GlobalParamListCollection.Count});
  77. }
  78. #region 属性通知
  79. /// <summary>
  80. /// Occurs when a property value changes.
  81. /// </summary>
  82. public event PropertyChangedEventHandler PropertyChanged;
  83. /// <summary>
  84. /// Checks if a property already matches a desired value. Sets the property and
  85. /// notifies listeners only when necessary.
  86. /// </summary>
  87. /// <typeparam name="T">Type of the property.</typeparam>
  88. /// <param name="storage">Reference to a property with both getter and setter.</param>
  89. /// <param name="value">Desired value for the property.</param>
  90. /// <param name="propertyName">Name of the property used to notify listeners. This
  91. /// value is optional and can be provided automatically when invoked from compilers that
  92. /// support CallerMemberName.</param>
  93. /// <returns>True if the value was changed, false if the existing value matched the
  94. /// desired value.</returns>
  95. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  96. {
  97. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  98. storage = value;
  99. RaisePropertyChanged(propertyName);
  100. return true;
  101. }
  102. /// <summary>
  103. /// Checks if a property already matches a desired value. Sets the property and
  104. /// notifies listeners only when necessary.
  105. /// </summary>
  106. /// <typeparam name="T">Type of the property.</typeparam>
  107. /// <param name="storage">Reference to a property with both getter and setter.</param>
  108. /// <param name="value">Desired value for the property.</param>
  109. /// <param name="propertyName">Name of the property used to notify listeners. This
  110. /// value is optional and can be provided automatically when invoked from compilers that
  111. /// support CallerMemberName.</param>
  112. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  113. /// <returns>True if the value was changed, false if the existing value matched the
  114. /// desired value.</returns>
  115. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  116. {
  117. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  118. storage = value;
  119. onChanged?.Invoke();
  120. RaisePropertyChanged(propertyName);
  121. return true;
  122. }
  123. /// <summary>
  124. /// Raises this object's PropertyChanged event.
  125. /// </summary>
  126. /// <param name="propertyName">Name of the property used to notify listeners. This
  127. /// value is optional and can be provided automatically when invoked from compilers
  128. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  129. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  130. {
  131. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  132. }
  133. /// <summary>
  134. /// Raises this object's PropertyChanged event.
  135. /// </summary>
  136. /// <param name="args">The PropertyChangedEventArgs</param>
  137. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  138. {
  139. PropertyChanged?.Invoke(this, args);
  140. }
  141. #endregion
  142. }
  143. }