NavigationBar.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. 
  2. using FWindSoft.Data;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Input;
  8. namespace FWindSoft.Wpf.Controls
  9. {
  10. /*
  11. * 暂时先不支持内容给窗体赋值
  12. */
  13. //[ContentProperty("WChildren")]
  14. [TemplatePart(Name = PART_Child, Type = typeof(NavigationBar))]
  15. public class NavigationBar : Control
  16. {
  17. #region 附加属性
  18. /// <summary>
  19. /// 窗体启动参数,根据Parameter在load中初始化
  20. /// </summary>
  21. public static readonly DependencyProperty ParameterProperty = DependencyProperty.RegisterAttached("Parameter",
  22. typeof(NParameter), typeof(NavigationBar), new PropertyMetadata(null, null));
  23. [AttachedPropertyBrowsableForType(typeof(NChildWindow))]
  24. public static NParameter GetParameter(DependencyObject obj)
  25. {
  26. return obj.GetValue(ParameterProperty) as NParameter;
  27. }
  28. [AttachedPropertyBrowsableForType(typeof(NChildWindow))]
  29. public static void SetParameter(DependencyObject obj, object value)
  30. {
  31. obj.SetValue(ParameterProperty, value);
  32. }
  33. #endregion
  34. #region 静态信息管理
  35. private static NavigationBar m_CurrentNavigation;
  36. /// <summary>
  37. /// 当前导航信息
  38. /// </summary>
  39. public static NavigationBar CurrentNavigation
  40. {
  41. get
  42. {
  43. if (m_CurrentNavigation == null)
  44. {
  45. m_CurrentNavigation = new NavigationBar();
  46. }
  47. return m_CurrentNavigation;
  48. }
  49. }
  50. #endregion
  51. static NavigationBar()
  52. {
  53. DefaultStyleKeyProperty.OverrideMetadata(typeof(NavigationBar),
  54. new FrameworkPropertyMetadata(typeof(NavigationBar)));
  55. Jump = new RoutedCommand("跳转", typeof(NavigationBar));
  56. CommandManager.RegisterClassCommandBinding(typeof(NavigationBar), new CommandBinding(Jump, OnJumpCommand, CanExecuteJump));
  57. GoBack = new RoutedCommand("返回", typeof(NavigationBar));
  58. CommandManager.RegisterClassCommandBinding(typeof(NavigationBar), new CommandBinding(GoBack, OnGoBackCommand, CanExecuteGoBack));
  59. }
  60. public NavigationBar()
  61. {
  62. NavigationItems = new ReadOnlyObservableCollection<NavigationItem>(m_NavigationItems = new ObservableCollection<NavigationItem>());
  63. }
  64. public const string PART_Child = "PART_Child";
  65. public override void OnApplyTemplate()
  66. {
  67. base.OnApplyTemplate();
  68. ChildContainer = GetTemplateChild(PART_Child) as ScrollViewer;
  69. }
  70. private ScrollViewer ChildContainer{ get; set; }
  71. #region 依赖属性
  72. public static readonly DependencyProperty ShowWindowProperty = DependencyProperty.Register("ShowWindow",
  73. typeof(NChildWindow),
  74. typeof(NavigationBar), new PropertyMetadata(null, null));
  75. /// <summary>
  76. /// 关联显示属性集合
  77. /// </summary>
  78. internal NChildWindow ShowWindow
  79. {
  80. get { return (NChildWindow)GetValue(ShowWindowProperty); }
  81. set { SetValue(ShowWindowProperty, value); }
  82. }
  83. private void RaiseChange()
  84. {
  85. ShowWindow = GetCurrentWindow();
  86. //ShowWindow.InvalidateVisual();
  87. this.GoBackVisible= this.NavigationItems != null && this.NavigationItems.Count > 1;
  88. if (this.NavigationItems != null)
  89. {
  90. this.NavigationItems.ToList().ForEach(i => i.Refresh());
  91. }
  92. }
  93. #endregion
  94. #region 操作与队列相似
  95. private ObservableCollection<NavigationItem> m_NavigationItems;
  96. public ReadOnlyObservableCollection<NavigationItem> NavigationItems
  97. {
  98. get;
  99. private set;
  100. }
  101. /// <summary>
  102. /// 添加导航项
  103. /// </summary>
  104. /// <param name="item"></param>
  105. private void AddNavigationItem(NavigationItem item)
  106. {
  107. this.m_NavigationItems.Add(item);
  108. RaiseChange();
  109. }
  110. /// <summary>
  111. /// 移除导航项
  112. /// </summary>
  113. /// <param name="item"></param>
  114. private void RemoveNavigationItem(NavigationItem item)
  115. {
  116. var useItem = this.m_NavigationItems.LastOrDefault(p => p.Window ==item.Window);
  117. if (useItem != null)
  118. {
  119. var index=this.m_NavigationItems.IndexOf(useItem);
  120. var allItems = this.m_NavigationItems.ToList();
  121. this.m_NavigationItems.Clear();
  122. for (int i = 0; i < index; i++)
  123. {
  124. this.m_NavigationItems.Add(allItems[i]);
  125. }
  126. RaiseChange();
  127. }
  128. }
  129. #endregion
  130. /*
  131. * 调用子窗体show方法,增加找到最近的一个赋值,也可以直接赋值
  132. * 调用窗体的Close方法,移除找到最近的一个赋值
  133. *
  134. * 导航样式为:当前:A-->B-->C
  135. */
  136. public void Remove(NChildWindow window)
  137. {
  138. var nParameter = GetParameter(window);
  139. if (nParameter != null && nParameter.IsClear)
  140. {
  141. this.m_NavigationItems.Clear();
  142. return;
  143. }
  144. RemoveNavigationItem(new NavigationItem(window));
  145. }
  146. public void Add(NChildWindow window)
  147. {
  148. var nParameter = GetParameter(window);
  149. if (nParameter != null && nParameter.IsClear)
  150. {
  151. this.m_NavigationItems.Clear();
  152. return;
  153. }
  154. AddNavigationItem(new NavigationItem(window));
  155. // RaiseChange();
  156. }
  157. /// <summary>
  158. /// 获取当前的窗体
  159. /// </summary>
  160. /// <returns></returns>
  161. public NChildWindow GetCurrentWindow()
  162. {
  163. if (this.m_NavigationItems == null || !this.m_NavigationItems.Any())
  164. return DefaultWindow;
  165. return this.m_NavigationItems.LastOrDefault().Window;
  166. }
  167. /// <summary>
  168. /// 默认窗体
  169. /// </summary>
  170. public NChildWindow DefaultWindow { get; set; }
  171. #region 跳转命令相关
  172. /// <summary>
  173. /// 跳转命令
  174. /// </summary>
  175. public static RoutedCommand Jump { get; private set; }
  176. private static void OnJumpCommand(object sender, ExecutedRoutedEventArgs e)
  177. {
  178. NavigationBar bar = sender as NavigationBar;
  179. if (bar != null)
  180. {
  181. NavigationItem item = e.Parameter as NavigationItem;
  182. //所谓跳转,移除跳转项目后面所有的选项
  183. var index=bar.NavigationItems.IndexOf(item);
  184. if (index < bar.NavigationItems.Count - 1)
  185. {
  186. bar.RemoveNavigationItem(bar.NavigationItems[index + 1]);
  187. }
  188. }
  189. }
  190. private static void CanExecuteJump(object sender, CanExecuteRoutedEventArgs e)
  191. {
  192. NavigationBar bar = sender as NavigationBar;
  193. if (bar != null)
  194. {
  195. NavigationItem item = e.Parameter as NavigationItem;
  196. if (item == null || bar.NavigationItems.Last() == item)
  197. {
  198. e.CanExecute = false;
  199. return;
  200. }
  201. }
  202. e.CanExecute = true;
  203. }
  204. #endregion
  205. #region 返回相关
  206. /// <summary>
  207. /// 跳转命令
  208. /// </summary>
  209. public static RoutedCommand GoBack { get; private set; }
  210. private static void OnGoBackCommand(object sender, ExecutedRoutedEventArgs e)
  211. {
  212. NavigationBar bar = sender as NavigationBar;
  213. if (bar != null)
  214. {
  215. if (bar.NavigationItems != null && bar.NavigationItems.Count > 1)
  216. {
  217. NavigationItem item = bar.NavigationItems[bar.NavigationItems.Count - 2];
  218. //所谓跳转,移除跳转项目后面所有的选项
  219. var index = bar.NavigationItems.IndexOf(item);
  220. if (index < bar.NavigationItems.Count - 1)
  221. {
  222. bar.RemoveNavigationItem(bar.NavigationItems[index + 1]);
  223. }
  224. }
  225. }
  226. }
  227. private static void CanExecuteGoBack(object sender, CanExecuteRoutedEventArgs e)
  228. {
  229. NavigationBar bar = sender as NavigationBar;
  230. if (bar != null)
  231. {
  232. e.CanExecute = bar.GoBackVisible;
  233. }
  234. e.CanExecute = true;
  235. }
  236. public static readonly DependencyProperty GoBackVisibleProperty = DependencyProperty.Register("GoBackVisible",
  237. typeof(bool),
  238. typeof(NavigationBar), new PropertyMetadata(false, null));
  239. /// <summary>
  240. /// 关联显示属性集合
  241. /// </summary>
  242. internal bool GoBackVisible
  243. {
  244. get { return (bool)GetValue(GoBackVisibleProperty); }
  245. set { SetValue(GoBackVisibleProperty, value); }
  246. }
  247. #endregion
  248. #region 逐步扩展
  249. /// <summary>
  250. /// 刷新存量窗体的加载状态【延迟到显示是起作用】
  251. /// </summary>
  252. public void RefreshWindows()
  253. {
  254. for (int i = 0; i < this.m_NavigationItems.Count; i++)
  255. {
  256. var item = this.m_NavigationItems[i];
  257. item.Window?.ResetLoadData();
  258. }
  259. }
  260. /// <summary>
  261. /// 刷新上一个窗体状态
  262. /// </summary>
  263. public void RefreshPreWinddow()
  264. {
  265. if (this.NavigationItems != null && this.NavigationItems.Count > 1)
  266. {
  267. NavigationItem item = this.NavigationItems[this.NavigationItems.Count - 2];
  268. item.Window.ResetLoadData();
  269. }
  270. }
  271. #endregion
  272. protected override void OnPreviewMouseWheel(MouseWheelEventArgs e)
  273. {
  274. base.OnPreviewMouseWheel(e);
  275. if (ChildContainer != null)
  276. {
  277. var uiElement = e.OriginalSource as UIElement;
  278. if (uiElement == null)
  279. {
  280. return;
  281. }
  282. var useScroll=uiElement.GetParentTypeSelf<ScrollViewer>();
  283. while (useScroll != null && useScroll.ComputedVerticalScrollBarVisibility == Visibility.Collapsed)
  284. {
  285. useScroll = useScroll.GetParentType<ScrollViewer>();
  286. }
  287. if (useScroll == ChildContainer)
  288. {
  289. var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
  290. eventArg.RoutedEvent = UIElement.MouseWheelEvent;
  291. eventArg.Source = this;
  292. ChildContainer.RaiseEvent(eventArg);
  293. }
  294. }
  295. }
  296. }
  297. /// <summary>
  298. /// 封装操作实体,直接将控件绑定到Items,将出现可是话树重叠的情况
  299. /// </summary>
  300. public class NavigationItem:BasePropertyChanged
  301. {
  302. internal NavigationItem(NChildWindow window)
  303. {
  304. this.Window = window;
  305. }
  306. internal NavigationItem()
  307. {
  308. }
  309. public void Refresh()
  310. {
  311. RaisePropertyChanged(nameof(WindowDisplay));
  312. }
  313. /// <summary>
  314. /// 导航窗体使用参数
  315. /// </summary>
  316. //public NParameter NParameter { get; set; }
  317. /// <summary>
  318. /// 关联窗体
  319. /// </summary>
  320. public NChildWindow Window { get; set; }
  321. /// <summary>
  322. /// 窗体描述
  323. /// </summary>
  324. public string WindowDisplay
  325. {
  326. get
  327. {
  328. if (Window == null)
  329. return "Null";
  330. return Window.Title;
  331. }
  332. }
  333. }
  334. }