终端一体化运控平台
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

221 rader
8.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace BPASmartClient.CustomResource.UserControls
  16. {
  17. /// <summary>
  18. /// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
  19. ///
  20. /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
  21. /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
  22. /// 元素中:
  23. ///
  24. /// xmlns:MyNamespace="clr-namespace:BPASmartClient.CustomResource.UserControls"
  25. ///
  26. ///
  27. /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
  28. /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
  29. /// 元素中:
  30. ///
  31. /// xmlns:MyNamespace="clr-namespace:BPASmartClient.CustomResource.UserControls;assembly=BPASmartClient.CustomResource.UserControls"
  32. ///
  33. /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,
  34. /// 并重新生成以避免编译错误:
  35. ///
  36. /// 在解决方案资源管理器中右击目标项目,然后依次单击
  37. /// “添加引用”->“项目”->[浏览查找并选择此项目]
  38. ///
  39. ///
  40. /// 步骤 2)
  41. /// 继续操作并在 XAML 文件中使用控件。
  42. ///
  43. /// <MyNamespace:IcoButton/>
  44. ///
  45. /// </summary>
  46. public class IcoButton : Button
  47. {
  48. static IcoButton()
  49. {
  50. DefaultStyleKeyProperty.OverrideMetadata(typeof(IcoButton), new FrameworkPropertyMetadata(typeof(IcoButton)));
  51. }
  52. private Brush tempBackground;
  53. private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  54. {
  55. (d as IcoButton)?.Refresh();
  56. }
  57. private void Refresh()
  58. {
  59. tempBackground = this.Background;
  60. }
  61. //public Action Command
  62. //{
  63. // get { return (Action)GetValue(CommandProperty); }
  64. // set { SetValue(CommandProperty, value); }
  65. //}
  66. //public static readonly DependencyProperty CommandProperty =
  67. // DependencyProperty.Register("Command", typeof(Action), typeof(IcoButton),
  68. // new PropertyMetadata(default, new PropertyChangedCallback(OnPropertyChanged)));
  69. //public Action<object> CommandObj
  70. //{
  71. // get { return (Action<object>)GetValue(CommandObjProperty); }
  72. // set { SetValue(CommandObjProperty, value); }
  73. //}
  74. //public static readonly DependencyProperty CommandObjProperty =
  75. // DependencyProperty.Register("CommandObj", typeof(Action<object>), typeof(IcoButton),
  76. // new PropertyMetadata(default, new PropertyChangedCallback(OnPropertyChanged)));
  77. //public object CommandPar
  78. //{
  79. // get { return (object)GetValue(CommandParProperty); }
  80. // set { SetValue(CommandParProperty, value); }
  81. //}
  82. //public static readonly DependencyProperty CommandParProperty =
  83. // DependencyProperty.Register("CommandPar", typeof(object), typeof(IcoButton),
  84. // new PropertyMetadata(default, new PropertyChangedCallback(OnPropertyChanged)));
  85. //public string Content
  86. //{
  87. // get { return (string)GetValue(ContentProperty); }
  88. // set { SetValue(ContentProperty, value); }
  89. //}
  90. //public static readonly DependencyProperty ContentProperty =
  91. // DependencyProperty.Register("Content", typeof(string), typeof(IcoButton),
  92. // new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnPropertyChanged)));
  93. public string IcoText
  94. {
  95. get { return (string)GetValue(IcoTextProperty); }
  96. set { SetValue(IcoTextProperty, value); }
  97. }
  98. public static readonly DependencyProperty IcoTextProperty =
  99. DependencyProperty.Register("IcoText", typeof(string), typeof(IcoButton),
  100. new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnPropertyChanged)));
  101. public Brush EnterBackground
  102. {
  103. get { return (Brush)GetValue(EnterBackgroundProperty); }
  104. set { SetValue(EnterBackgroundProperty, value); }
  105. }
  106. public static readonly DependencyProperty EnterBackgroundProperty =
  107. DependencyProperty.Register("EnterBackground", typeof(Brush), typeof(IcoButton),
  108. new PropertyMetadata(Brushes.DarkGray, new PropertyChangedCallback(OnPropertyChanged)));
  109. public Brush PressedBackground
  110. {
  111. get { return (Brush)GetValue(PressedBackgroundProperty); }
  112. set { SetValue(PressedBackgroundProperty, value); }
  113. }
  114. public static readonly DependencyProperty PressedBackgroundProperty =
  115. DependencyProperty.Register("PressedBackground", typeof(Brush), typeof(IcoButton),
  116. new PropertyMetadata(Brushes.Gray, new PropertyChangedCallback(OnPropertyChanged)));
  117. public Brush EnableColor
  118. {
  119. get { return (Brush)GetValue(EnableColorProperty); }
  120. set { SetValue(EnableColorProperty, value); }
  121. }
  122. public static readonly DependencyProperty EnableColorProperty =
  123. DependencyProperty.Register("EnableColor", typeof(Brush), typeof(IcoButton),
  124. new PropertyMetadata(Brushes.Gray, new PropertyChangedCallback(OnPropertyChanged)));
  125. public override void OnApplyTemplate()
  126. {
  127. base.OnApplyTemplate();
  128. var icon = base.GetTemplateChild("PART_icoText") as TextBlock;
  129. var content = base.GetTemplateChild("PART_content") as TextBlock;
  130. var gr = base.GetTemplateChild("PART_gr") as Grid;
  131. //if (!this.IsEnabled)
  132. //{
  133. // this.Foreground = EnableColor;
  134. // this.BorderBrush = EnableColor;
  135. //}
  136. //this.IsEnabledChanged += IcoButton_IsEnabledChanged;
  137. if (icon != null)
  138. {
  139. Binding binding = new Binding("IcoText");
  140. binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
  141. binding.RelativeSource = new RelativeSource() { AncestorType = typeof(IcoButton), Mode = RelativeSourceMode.FindAncestor };
  142. icon.SetBinding(TextBlock.TextProperty, binding);
  143. }
  144. if (content != null)
  145. {
  146. Binding binding = new Binding("Content");
  147. binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
  148. binding.RelativeSource = new RelativeSource() { AncestorType = typeof(IcoButton), Mode = RelativeSourceMode.FindAncestor };
  149. content.SetBinding(TextBlock.TextProperty, binding);
  150. }
  151. if (gr != null)
  152. {
  153. //gr.MouseLeftButtonDown += Gr_MouseLeftButtonDown;
  154. gr.MouseEnter += Gr_MouseEnter;
  155. gr.MouseLeave += Gr_MouseLeave;
  156. }
  157. }
  158. private void IcoButton_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
  159. {
  160. //if (!this.IsEnabled)
  161. //{
  162. // this.Foreground = EnableColor;
  163. // this.BorderBrush = EnableColor;
  164. //}
  165. }
  166. /// <summary>
  167. /// 鼠标离开颜色
  168. /// </summary>
  169. /// <param name="sender"></param>
  170. /// <param name="e"></param>
  171. private void Gr_MouseLeave(object sender, MouseEventArgs e)
  172. {
  173. //VisualStateManager.GoToState(this, "Leave", true);
  174. this.Background = tempBackground;
  175. }
  176. /// <summary>
  177. /// 鼠标进入颜色
  178. /// </summary>
  179. /// <param name="sender"></param>
  180. /// <param name="e"></param>
  181. private void Gr_MouseEnter(object sender, MouseEventArgs e)
  182. {
  183. //VisualStateManager.GoToState(this, "Enter", true);
  184. this.Background = EnterBackground;
  185. }
  186. private void Gr_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  187. {
  188. //Command?.Invoke();
  189. //this.Background = PressedBackground;
  190. }
  191. }
  192. }