ParameterEdit.xaml.cs 6.1 KB

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