终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IcoButton.cs 8.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.Transparent, 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?.Execute(null);
  189. //this.Background = PressedBackground;
  190. }
  191. }
  192. }