123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
-
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Markup;
- using System.Windows.Threading;
- using FWindSoft.Data;
- namespace FWindSoft.Wpf.Controls
- {
- /// <summary>
- /// 导航窗体
- /// </summary>
- [ContentProperty("Content")]
- public class NChildWindow:UserControl
- {
- public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title",
- typeof(string),
- typeof(NChildWindow), new PropertyMetadata(string.Empty));
- public static readonly DependencyProperty AttachElementProperty = DependencyProperty.Register("AttachElement",
- typeof(UIElement),
- typeof(NChildWindow), new PropertyMetadata(null));
- public NChildWindow()
- {
- BindingWidth();
- this.Loaded+=NChildWindow_Loaded;
- this.Unloaded+=NChildWindow_Unloaded;
- //AttachElements = new ObservableCollection<UIElement>();
- }
- #region 事件封装
- private static void NChildWindow_Unloaded(object sender, RoutedEventArgs e)
- {
- NChildWindow nW = sender as NChildWindow;
- if (nW == null)
- return;
- nW.OnClose(new NChildWindowArgs(e) { RefParameter = NavigationBar.GetParameter(nW) });
- }
- private static void NChildWindow_Loaded(object sender, RoutedEventArgs e)
- {
- NChildWindow nW = sender as NChildWindow;
- if (nW == null)
- return;
- nW.OnLoad(new NChildWindowArgs(e) { RefParameter = NavigationBar.GetParameter(nW) });
- }
- #endregion
- protected virtual void OnLoad(NChildWindowArgs args )
- {
- if (EveryTimeLoad || !m_IsLoadData)
- {
- if (OnceBreakLoad.Value)
- {
- return;
- }
- this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => {
- if (args.RefParameter != null)
- {
- LoadData(args.RefParameter.StartParameter);
- }
- else
- {
- LoadData(null);
- }
- m_IsLoadData = true;
- }));
-
- }
- }
- protected virtual void OnClose(NChildWindowArgs args)
- {
- }
- /// <summary>
- /// 窗口名称
- /// </summary>
- public string Title
- {
- get { return this.GetValue(TitleProperty) as string; }
- set { this.SetValue(TitleProperty,value); }
- }
- #region 打开窗体
- public virtual void Show()
- {
- if (NavigationBar.CurrentNavigation != null)
- {
- NavigationBar.CurrentNavigation.Add(this);
- }
- }
- public virtual void Show(NavigationBar bar)
- {
- if (bar != null)
- {
- bar.Add(this);
- }
- }
- public virtual void Show(UIElement parent)
- {
- var bar = parent.GetParentType<NavigationBar>();
- if (bar != null)
- {
- bar.Add(this);
- }
- }
- public void Show(NParameter parameter)
- {
- NavigationBar.SetParameter(this, parameter);
- Show();
- }
- #endregion
- #region 加载信息模块
- private bool m_IsLoadData = false;
- public void ResetLoadData()
- {
- this.m_IsLoadData = false;
- }
- /// <summary>
- /// 每次都加载
- /// </summary>
- public bool EveryTimeLoad { get; protected set; }
- /// <summary>
- /// 是否打断一次加载【首次加载也可打断】
- /// </summary>
- public MomentVariable<bool> OnceBreakLoad { get; private set; } = new MomentVariable<bool>(false);
- protected virtual void LoadData(LoadParameter parameter)
- {
- }
- #endregion
- public void SetStartParameter(NParameter parameter)
- {
- NavigationBar.SetParameter(this, parameter);
- }
- public virtual void Close()
- {
- if (NavigationBar.CurrentNavigation != null)
- {
- NavigationBar.CurrentNavigation.Remove(this);
- }
- }
- public void Close(NParameter parameter)
- {
- NavigationBar.SetParameter(this, parameter);
- Close();
- }
-
- /// <summary>
- /// 附加元素
- /// </summary>
- public UIElement AttachElement
- {
- get { return this.GetValue(AttachElementProperty) as UIElement; }
- set { this.SetValue(AttachElementProperty, value); }
- }
- #region 附加功能
- private void BindingWidth()
- {
- RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor);
- rs.AncestorType = typeof(ScrollContentPresenter);
- Binding binding = new Binding("ActualWidth") { RelativeSource = rs };
- this.SetBinding(WidthProperty, binding);
- }
- #endregion
- }
- public class NChildWindowArgs : RoutedEventArgs
- {
- public NChildWindowArgs(RoutedEventArgs args)
- {
- this.RoutedEvent = args.RoutedEvent;//他的顺序要在handled赋值之上
- this.Source = args.Source;
- this.Handled = args.Handled;
-
-
- }
- /// <summary>
- /// 关联参数
- /// </summary>
- public NParameter RefParameter { get; set; }
- }
- }
|