AddProduct.xaml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Security.Permissions;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace TeamAAS_VP.Controls
  19. {
  20. /// <summary>
  21. /// AddProduct.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class AddProduct : UserControl, INotifyPropertyChanged
  24. {
  25. public AddProduct()
  26. {
  27. InitializeComponent();
  28. DataContext = this;
  29. }
  30. private string _Title;
  31. public string Title
  32. {
  33. get { return _Title; }
  34. set { SetProperty(ref _Title,value); }
  35. }
  36. private string _Item;
  37. public string Item
  38. {
  39. get { return _Item; }
  40. set { SetProperty(ref _Item, value); }
  41. }
  42. private string _ProductName;
  43. public string ProductName
  44. {
  45. get { return _ProductName; }
  46. set { SetProperty(ref _ProductName,value); }
  47. }
  48. #region 属性通知
  49. /// <summary>
  50. /// Occurs when a property value changes.
  51. /// </summary>
  52. public event PropertyChangedEventHandler PropertyChanged;
  53. /// <summary>
  54. /// Checks if a property already matches a desired value. Sets the property and
  55. /// notifies listeners only when necessary.
  56. /// </summary>
  57. /// <typeparam name="T">Type of the property.</typeparam>
  58. /// <param name="storage">Reference to a property with both getter and setter.</param>
  59. /// <param name="value">Desired value for the property.</param>
  60. /// <param name="propertyName">Name of the property used to notify listeners. This
  61. /// value is optional and can be provided automatically when invoked from compilers that
  62. /// support CallerMemberName.</param>
  63. /// <returns>True if the value was changed, false if the existing value matched the
  64. /// desired value.</returns>
  65. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  66. {
  67. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  68. storage = value;
  69. RaisePropertyChanged(propertyName);
  70. return true;
  71. }
  72. /// <summary>
  73. /// Checks if a property already matches a desired value. Sets the property and
  74. /// notifies listeners only when necessary.
  75. /// </summary>
  76. /// <typeparam name="T">Type of the property.</typeparam>
  77. /// <param name="storage">Reference to a property with both getter and setter.</param>
  78. /// <param name="value">Desired value for the property.</param>
  79. /// <param name="propertyName">Name of the property used to notify listeners. This
  80. /// value is optional and can be provided automatically when invoked from compilers that
  81. /// support CallerMemberName.</param>
  82. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  83. /// <returns>True if the value was changed, false if the existing value matched the
  84. /// desired value.</returns>
  85. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  86. {
  87. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  88. storage = value;
  89. onChanged?.Invoke();
  90. RaisePropertyChanged(propertyName);
  91. return true;
  92. }
  93. /// <summary>
  94. /// Raises this object's PropertyChanged event.
  95. /// </summary>
  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
  98. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  99. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  100. {
  101. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  102. }
  103. /// <summary>
  104. /// Raises this object's PropertyChanged event.
  105. /// </summary>
  106. /// <param name="args">The PropertyChangedEventArgs</param>
  107. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  108. {
  109. PropertyChanged?.Invoke(this, args);
  110. }
  111. #endregion
  112. }
  113. }