123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
-
- using FWindSoft.Data;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- namespace FWindSoft.Wpf.Controls
- {
- /*
- * 暂时先不支持内容给窗体赋值
- */
- //[ContentProperty("WChildren")]
- [TemplatePart(Name = PART_Child, Type = typeof(NavigationBar))]
- public class NavigationBar : Control
- {
- #region 附加属性
- /// <summary>
- /// 窗体启动参数,根据Parameter在load中初始化
- /// </summary>
- public static readonly DependencyProperty ParameterProperty = DependencyProperty.RegisterAttached("Parameter",
- typeof(NParameter), typeof(NavigationBar), new PropertyMetadata(null, null));
- [AttachedPropertyBrowsableForType(typeof(NChildWindow))]
- public static NParameter GetParameter(DependencyObject obj)
- {
- return obj.GetValue(ParameterProperty) as NParameter;
- }
- [AttachedPropertyBrowsableForType(typeof(NChildWindow))]
- public static void SetParameter(DependencyObject obj, object value)
- {
- obj.SetValue(ParameterProperty, value);
- }
- #endregion
- #region 静态信息管理
- private static NavigationBar m_CurrentNavigation;
- /// <summary>
- /// 当前导航信息
- /// </summary>
- public static NavigationBar CurrentNavigation
- {
- get
- {
- if (m_CurrentNavigation == null)
- {
- m_CurrentNavigation = new NavigationBar();
- }
- return m_CurrentNavigation;
- }
- }
- #endregion
- static NavigationBar()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(NavigationBar),
- new FrameworkPropertyMetadata(typeof(NavigationBar)));
- Jump = new RoutedCommand("跳转", typeof(NavigationBar));
- CommandManager.RegisterClassCommandBinding(typeof(NavigationBar), new CommandBinding(Jump, OnJumpCommand, CanExecuteJump));
- GoBack = new RoutedCommand("返回", typeof(NavigationBar));
- CommandManager.RegisterClassCommandBinding(typeof(NavigationBar), new CommandBinding(GoBack, OnGoBackCommand, CanExecuteGoBack));
- }
- public NavigationBar()
- {
- NavigationItems = new ReadOnlyObservableCollection<NavigationItem>(m_NavigationItems = new ObservableCollection<NavigationItem>());
- }
- public const string PART_Child = "PART_Child";
- public override void OnApplyTemplate()
- {
- base.OnApplyTemplate();
- ChildContainer = GetTemplateChild(PART_Child) as ScrollViewer;
- }
- private ScrollViewer ChildContainer{ get; set; }
- #region 依赖属性
- public static readonly DependencyProperty ShowWindowProperty = DependencyProperty.Register("ShowWindow",
- typeof(NChildWindow),
- typeof(NavigationBar), new PropertyMetadata(null, null));
-
- /// <summary>
- /// 关联显示属性集合
- /// </summary>
- internal NChildWindow ShowWindow
- {
- get { return (NChildWindow)GetValue(ShowWindowProperty); }
- set { SetValue(ShowWindowProperty, value); }
- }
- private void RaiseChange()
- {
- ShowWindow = GetCurrentWindow();
- //ShowWindow.InvalidateVisual();
- this.GoBackVisible= this.NavigationItems != null && this.NavigationItems.Count > 1;
- if (this.NavigationItems != null)
- {
- this.NavigationItems.ToList().ForEach(i => i.Refresh());
- }
- }
- #endregion
- #region 操作与队列相似
- private ObservableCollection<NavigationItem> m_NavigationItems;
- public ReadOnlyObservableCollection<NavigationItem> NavigationItems
- {
- get;
- private set;
- }
- /// <summary>
- /// 添加导航项
- /// </summary>
- /// <param name="item"></param>
- private void AddNavigationItem(NavigationItem item)
- {
- this.m_NavigationItems.Add(item);
- RaiseChange();
- }
- /// <summary>
- /// 移除导航项
- /// </summary>
- /// <param name="item"></param>
- private void RemoveNavigationItem(NavigationItem item)
- {
- var useItem = this.m_NavigationItems.LastOrDefault(p => p.Window ==item.Window);
- if (useItem != null)
- {
- var index=this.m_NavigationItems.IndexOf(useItem);
- var allItems = this.m_NavigationItems.ToList();
- this.m_NavigationItems.Clear();
- for (int i = 0; i < index; i++)
- {
- this.m_NavigationItems.Add(allItems[i]);
- }
- RaiseChange();
- }
-
- }
- #endregion
- /*
- * 调用子窗体show方法,增加找到最近的一个赋值,也可以直接赋值
- * 调用窗体的Close方法,移除找到最近的一个赋值
- *
- * 导航样式为:当前:A-->B-->C
- */
- public void Remove(NChildWindow window)
- {
- var nParameter = GetParameter(window);
- if (nParameter != null && nParameter.IsClear)
- {
- this.m_NavigationItems.Clear();
- return;
- }
- RemoveNavigationItem(new NavigationItem(window));
-
- }
- public void Add(NChildWindow window)
- {
- var nParameter = GetParameter(window);
- if (nParameter != null && nParameter.IsClear)
- {
- this.m_NavigationItems.Clear();
- return;
- }
- AddNavigationItem(new NavigationItem(window));
- // RaiseChange();
- }
- /// <summary>
- /// 获取当前的窗体
- /// </summary>
- /// <returns></returns>
- public NChildWindow GetCurrentWindow()
- {
- if (this.m_NavigationItems == null || !this.m_NavigationItems.Any())
- return DefaultWindow;
- return this.m_NavigationItems.LastOrDefault().Window;
- }
- /// <summary>
- /// 默认窗体
- /// </summary>
- public NChildWindow DefaultWindow { get; set; }
- #region 跳转命令相关
- /// <summary>
- /// 跳转命令
- /// </summary>
- public static RoutedCommand Jump { get; private set; }
- private static void OnJumpCommand(object sender, ExecutedRoutedEventArgs e)
- {
- NavigationBar bar = sender as NavigationBar;
- if (bar != null)
- {
- NavigationItem item = e.Parameter as NavigationItem;
- //所谓跳转,移除跳转项目后面所有的选项
- var index=bar.NavigationItems.IndexOf(item);
- if (index < bar.NavigationItems.Count - 1)
- {
- bar.RemoveNavigationItem(bar.NavigationItems[index + 1]);
- }
- }
- }
- private static void CanExecuteJump(object sender, CanExecuteRoutedEventArgs e)
- {
- NavigationBar bar = sender as NavigationBar;
- if (bar != null)
- {
- NavigationItem item = e.Parameter as NavigationItem;
- if (item == null || bar.NavigationItems.Last() == item)
- {
- e.CanExecute = false;
- return;
- }
-
- }
- e.CanExecute = true;
- }
- #endregion
- #region 返回相关
- /// <summary>
- /// 跳转命令
- /// </summary>
- public static RoutedCommand GoBack { get; private set; }
- private static void OnGoBackCommand(object sender, ExecutedRoutedEventArgs e)
- {
- NavigationBar bar = sender as NavigationBar;
- if (bar != null)
- {
- if (bar.NavigationItems != null && bar.NavigationItems.Count > 1)
- {
- NavigationItem item = bar.NavigationItems[bar.NavigationItems.Count - 2];
- //所谓跳转,移除跳转项目后面所有的选项
- var index = bar.NavigationItems.IndexOf(item);
- if (index < bar.NavigationItems.Count - 1)
- {
- bar.RemoveNavigationItem(bar.NavigationItems[index + 1]);
- }
- }
-
- }
- }
- private static void CanExecuteGoBack(object sender, CanExecuteRoutedEventArgs e)
- {
- NavigationBar bar = sender as NavigationBar;
- if (bar != null)
- {
- e.CanExecute = bar.GoBackVisible;
- }
- e.CanExecute = true;
-
- }
- public static readonly DependencyProperty GoBackVisibleProperty = DependencyProperty.Register("GoBackVisible",
- typeof(bool),
- typeof(NavigationBar), new PropertyMetadata(false, null));
- /// <summary>
- /// 关联显示属性集合
- /// </summary>
- internal bool GoBackVisible
- {
- get { return (bool)GetValue(GoBackVisibleProperty); }
- set { SetValue(GoBackVisibleProperty, value); }
- }
- #endregion
- #region 逐步扩展
- /// <summary>
- /// 刷新存量窗体的加载状态【延迟到显示是起作用】
- /// </summary>
- public void RefreshWindows()
- {
- for (int i = 0; i < this.m_NavigationItems.Count; i++)
- {
- var item = this.m_NavigationItems[i];
- item.Window?.ResetLoadData();
- }
- }
- /// <summary>
- /// 刷新上一个窗体状态
- /// </summary>
- public void RefreshPreWinddow()
- {
- if (this.NavigationItems != null && this.NavigationItems.Count > 1)
- {
- NavigationItem item = this.NavigationItems[this.NavigationItems.Count - 2];
- item.Window.ResetLoadData();
- }
- }
- #endregion
- protected override void OnPreviewMouseWheel(MouseWheelEventArgs e)
- {
- base.OnPreviewMouseWheel(e);
- if (ChildContainer != null)
- {
- var uiElement = e.OriginalSource as UIElement;
- if (uiElement == null)
- {
- return;
- }
- var useScroll=uiElement.GetParentTypeSelf<ScrollViewer>();
- while (useScroll != null && useScroll.ComputedVerticalScrollBarVisibility == Visibility.Collapsed)
- {
- useScroll = useScroll.GetParentType<ScrollViewer>();
- }
- if (useScroll == ChildContainer)
- {
- var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
- eventArg.RoutedEvent = UIElement.MouseWheelEvent;
- eventArg.Source = this;
- ChildContainer.RaiseEvent(eventArg);
- }
- }
-
- }
- }
- /// <summary>
- /// 封装操作实体,直接将控件绑定到Items,将出现可是话树重叠的情况
- /// </summary>
- public class NavigationItem:BasePropertyChanged
- {
- internal NavigationItem(NChildWindow window)
- {
- this.Window = window;
- }
- internal NavigationItem()
- {
- }
- public void Refresh()
- {
- RaisePropertyChanged(nameof(WindowDisplay));
- }
- /// <summary>
- /// 导航窗体使用参数
- /// </summary>
- //public NParameter NParameter { get; set; }
- /// <summary>
- /// 关联窗体
- /// </summary>
- public NChildWindow Window { get; set; }
- /// <summary>
- /// 窗体描述
- /// </summary>
- public string WindowDisplay
- {
- get
- {
- if (Window == null)
- return "Null";
- return Window.Title;
- }
- }
-
- }
- }
|