OrderTabControl.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Controls.Primitives;
  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 Muchinfo.WPF.Controls
  16. {
  17. /// <summary>
  18. /// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
  19. ///
  20. /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
  21. /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
  22. /// 元素中:
  23. ///
  24. /// xmlns:MyNamespace="clr-namespace:Muchinfo.MTPClient.Account.Views"
  25. ///
  26. ///
  27. /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
  28. /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
  29. /// 元素中:
  30. ///
  31. /// xmlns:MyNamespace="clr-namespace:Muchinfo.MTPClient.Account.Views;assembly=Muchinfo.MTPClient.Account.Views"
  32. ///
  33. /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,
  34. /// 并重新生成以避免编译错误:
  35. ///
  36. /// 在解决方案资源管理器中右击目标项目,然后依次单击
  37. /// “添加引用”->“项目”->[浏览查找并选择此项目]
  38. ///
  39. ///
  40. /// 步骤 2)
  41. /// 继续操作并在 XAML 文件中使用控件。
  42. ///
  43. /// <MyNamespace:OrderTabControl/>
  44. ///
  45. /// </summary>
  46. // [TemplatePart(Name = "XscrollViewer", Type = typeof(ScrollViewer))]
  47. [TemplatePart(Name = "PART_HorizontalScrollBar", Type = typeof(ScrollBar))]
  48. [TemplatePart(Name = "PART_SelectedContentHost", Type = typeof(ContentPresenter))]
  49. [TemplatePart(Name = "Part_TabViewer", Type = typeof(ScrollViewer))]
  50. public class OrderTabControl : TabControl
  51. {
  52. static OrderTabControl()
  53. {
  54. DefaultStyleKeyProperty.OverrideMetadata(typeof(OrderTabControl), new FrameworkPropertyMetadata(typeof(OrderTabControl)));
  55. }
  56. public OrderTabControl()
  57. {
  58. this.LayoutUpdated += OrderTabControl_LayoutUpdated;
  59. }
  60. void OrderTabControl_LayoutUpdated(object sender, EventArgs e)
  61. {
  62. // SetScrollBar();
  63. }
  64. private ScrollViewer scrollViewer = null;
  65. private ScrollBar scrollBar = null;
  66. private ContentPresenter presenter = null;
  67. private bool _isSelectedChange = false;
  68. public override void OnApplyTemplate()
  69. {
  70. base.OnApplyTemplate();
  71. // scrollViewer = this.GetTemplateChild("XscrollViewer") as ScrollViewer;
  72. scrollBar = this.GetTemplateChild("PART_HorizontalScrollBar") as ScrollBar;
  73. scrollViewer = this.GetTemplateChild("Part_TabViewer") as ScrollViewer;
  74. if (scrollBar != null)
  75. {
  76. scrollBar.ValueChanged+=scrollBar_ValueChanged;
  77. }
  78. }
  79. /// <summary>
  80. /// 横向滚动条的值
  81. /// </summary>
  82. public double HSValue
  83. {
  84. get { return (double)GetValue(HSValueProperty); }
  85. set { SetValue(HSValueProperty, value); }
  86. }
  87. // Using a DependencyProperty as the backing store for HSValue. This enables animation, styling, binding, etc...
  88. public static readonly DependencyProperty HSValueProperty =
  89. DependencyProperty.Register("HSValue", typeof(double), typeof(OrderTabControl), new PropertyMetadata(0d,HSValueChangecalback));
  90. /// <summary>
  91. /// 横向滚动条
  92. /// </summary>
  93. public double ViewportWidth
  94. {
  95. get { return (double)GetValue(ViewportWidthProperty); }
  96. set { SetValue(ViewportWidthProperty, value); }
  97. }
  98. // Using a DependencyProperty as the backing store for ViewportWidth. This enables animation, styling, binding, etc...
  99. public static readonly DependencyProperty ViewportWidthProperty =
  100. DependencyProperty.Register("ViewportWidth", typeof(double), typeof(OrderTabControl), new PropertyMetadata(0d));
  101. public double ScrollableWidth
  102. {
  103. get { return (double)GetValue(ScrollableWidthProperty); }
  104. set { SetValue(ScrollableWidthProperty, value); }
  105. }
  106. // Using a DependencyProperty as the backing store for ScrollableWidth. This enables animation, styling, binding, etc...
  107. public static readonly DependencyProperty ScrollableWidthProperty =
  108. DependencyProperty.Register("ScrollableWidth", typeof(double), typeof(OrderTabControl), new PropertyMetadata(0d));
  109. private static void HSValueChangecalback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  110. {
  111. var oTabControl = d as OrderTabControl;
  112. if (oTabControl != null && oTabControl.scrollViewer!=null)
  113. {
  114. oTabControl.scrollViewer.ScrollToHorizontalOffset((double)e.NewValue);
  115. }
  116. }
  117. private void scrollBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  118. {
  119. if (scrollViewer != null)
  120. {
  121. scrollViewer.ScrollToHorizontalOffset((double)e.NewValue);
  122. }
  123. }
  124. protected override void OnRenderSizeChanged(SizeChangedInfo info)
  125. {
  126. base.OnRenderSizeChanged(info);
  127. //SetScrollBar();
  128. }
  129. /// <summary>
  130. /// 设置滚动条
  131. /// </summary>
  132. private void SetScrollBar()
  133. {
  134. var uielement = this.SelectedContent as UIElement;
  135. if (uielement != null)
  136. {
  137. scrollViewer = FindVisualChild<ScrollViewer>(uielement);
  138. }
  139. if (scrollViewer != null && scrollBar != null)
  140. {
  141. //scrollBar.Value = scrollViewer.HorizontalOffset;
  142. //scrollBar.Maximum = scrollViewer.ScrollableWidth;
  143. //scrollBar.ViewportSize = scrollViewer.ViewportWidth;
  144. ViewportWidth = scrollViewer.ViewportWidth;
  145. ScrollableWidth = scrollViewer.ScrollableWidth;
  146. HSValue = scrollViewer.HorizontalOffset;
  147. }
  148. }
  149. /// <summary>
  150. /// 从父容器中查找指定类型的控件
  151. /// </summary>
  152. /// <typeparam name="T"></typeparam>
  153. /// <param name="parent"></param>
  154. /// <returns></returns>
  155. private T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
  156. {
  157. if (parent == null) return null;
  158. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
  159. {
  160. var childObject = VisualTreeHelper.GetChild(parent, i);
  161. if (childObject == null) return null;
  162. var child = childObject as T;
  163. if (child == null)
  164. {
  165. var childItem = FindVisualChild<T>(childObject);
  166. if (childItem != null) return childItem;
  167. }
  168. return child;
  169. }
  170. return null;
  171. }
  172. /// <summary>
  173. /// 查找指定类型的父容器
  174. /// </summary>
  175. /// <typeparam name="T"></typeparam>
  176. /// <param name="child">The child.</param>
  177. /// <returns>``0.</returns>
  178. private static T FindVisualParent<T>(DependencyObject child) where T : DependencyObject
  179. {
  180. if (child == null) return null;
  181. var parentObject = VisualTreeHelper.GetParent(child);
  182. if (parentObject == null) return null;
  183. var parent = parentObject as T;
  184. return parent ?? FindVisualParent<T>(parentObject);
  185. }
  186. }
  187. }