终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

IcoButton.cs 6.5 KiB

pirms 2 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 : Control
  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 string Content
  70. {
  71. get { return (string)GetValue(ContentProperty); }
  72. set { SetValue(ContentProperty, value); }
  73. }
  74. public static readonly DependencyProperty ContentProperty =
  75. DependencyProperty.Register("Content", typeof(string), typeof(IcoButton),
  76. new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnPropertyChanged)));
  77. public string IcoText
  78. {
  79. get { return (string)GetValue(IcoTextProperty); }
  80. set { SetValue(IcoTextProperty, value); }
  81. }
  82. public static readonly DependencyProperty IcoTextProperty =
  83. DependencyProperty.Register("IcoText", typeof(string), typeof(IcoButton),
  84. new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnPropertyChanged)));
  85. public Brush EnterBackground
  86. {
  87. get { return (Brush)GetValue(EnterBackgroundProperty); }
  88. set { SetValue(EnterBackgroundProperty, value); }
  89. }
  90. public static readonly DependencyProperty EnterBackgroundProperty =
  91. DependencyProperty.Register("EnterBackground", typeof(Brush), typeof(IcoButton),
  92. new PropertyMetadata(Brushes.DarkGray, new PropertyChangedCallback(OnPropertyChanged)));
  93. public Brush PressedBackground
  94. {
  95. get { return (Brush)GetValue(PressedBackgroundProperty); }
  96. set { SetValue(PressedBackgroundProperty, value); }
  97. }
  98. public static readonly DependencyProperty PressedBackgroundProperty =
  99. DependencyProperty.Register("PressedBackground", typeof(Brush), typeof(IcoButton),
  100. new PropertyMetadata(Brushes.Gray, new PropertyChangedCallback(OnPropertyChanged)));
  101. public override void OnApplyTemplate()
  102. {
  103. base.OnApplyTemplate();
  104. var icon = base.GetTemplateChild("PART_icoText") as TextBlock;
  105. var content = base.GetTemplateChild("PART_content") as TextBlock;
  106. var gr = base.GetTemplateChild("PART_gr") as Grid;
  107. if (icon != null)
  108. {
  109. Binding binding = new Binding("IcoText");
  110. binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
  111. binding.RelativeSource = new RelativeSource() { AncestorType = typeof(IcoButton), Mode = RelativeSourceMode.FindAncestor };
  112. icon.SetBinding(TextBlock.TextProperty, binding);
  113. }
  114. if (content != null)
  115. {
  116. Binding binding = new Binding("Content");
  117. binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
  118. binding.RelativeSource = new RelativeSource() { AncestorType = typeof(IcoButton), Mode = RelativeSourceMode.FindAncestor };
  119. content.SetBinding(TextBlock.TextProperty, binding);
  120. }
  121. if (gr != null)
  122. {
  123. gr.MouseLeftButtonDown += Gr_MouseLeftButtonDown;
  124. gr.MouseEnter += Gr_MouseEnter;
  125. gr.MouseLeave += Gr_MouseLeave;
  126. }
  127. }
  128. /// <summary>
  129. /// 鼠标离开颜色
  130. /// </summary>
  131. /// <param name="sender"></param>
  132. /// <param name="e"></param>
  133. private void Gr_MouseLeave(object sender, MouseEventArgs e)
  134. {
  135. //VisualStateManager.GoToState(this, "Leave", true);
  136. this.Background = tempBackground;
  137. }
  138. /// <summary>
  139. /// 鼠标进入颜色
  140. /// </summary>
  141. /// <param name="sender"></param>
  142. /// <param name="e"></param>
  143. private void Gr_MouseEnter(object sender, MouseEventArgs e)
  144. {
  145. //VisualStateManager.GoToState(this, "Enter", true);
  146. this.Background = EnterBackground;
  147. }
  148. private void Gr_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  149. {
  150. if (Command != null) Command();
  151. this.Background = PressedBackground;
  152. }
  153. }
  154. }