using MuchInfo.Chart.Infrastructure.Helpers; using System; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows.Media; namespace MuchInfo.Chart.WPF.Controls.Utilities { /// /// PopupMenu.xaml 的交互逻辑 /// public partial class PopupMenu : UserControl { #region Fields private static Popup _cachPopup; private Popup _Popup; private Panel _popupRoot; #endregion Fields #region Constructors public PopupMenu(Panel popupRoot) { _popupRoot = popupRoot; this.InitializeComponent(); this.Padding = new Thickness(5.0); this.MouseLeftButtonDown -= PopupMenu_MouseLeftButtonDown; this.MouseLeftButtonDown += PopupMenu_MouseLeftButtonDown; } #endregion Constructors #region Delegates public delegate void PopupClosedEventHandler(); #endregion Delegates #region Events public event PopupMenu.PopupClosedEventHandler PopupClosed; #endregion Events #region Properties #region Public Properties public virtual Popup Popup { get { return this._Popup; } set { var eventHandler = new EventHandler(this.Popup_Closed); bool flag = this._Popup != null; if (flag) { this._Popup.Closed -= eventHandler; } this._Popup = value; flag = (this._Popup != null); if (flag) { this._Popup.Closed += eventHandler; } } } #endregion Public Properties #endregion Properties #region Methods #region Public Static Methods public static bool MenuIsUp() { return PopupMenu._cachPopup != null; } #endregion Public Static Methods #region Public Methods public void AddItem(string txt, ImageSource image, EventHandler onClick) { var chartMenuItem = new ChartMenuItem(txt, image, onClick); chartMenuItem.SetPopupMenu(this); this.MainStack.Children.Add(chartMenuItem); } public void AddItem(string txt, EventHandler onClick) { var chartMenuItem = new ChartMenuItem(txt, onClick); chartMenuItem.SetPopupMenu(this); this.MainStack.Children.Add(chartMenuItem); } public void AddItem(ChartMenuItem item) { item.SetPopupMenu(this); this.MainStack.Children.Add(item); } public void AddSeperator() { var seperator = new ChartMenuSeperator(); this.MainStack.Children.Add(seperator); } public void Show(MouseEventArgs aPos) { var offset = new Point(0.0, 0.0); this.Show(aPos, offset); } public void Show(Point aPos) { if (PopupMenu._cachPopup != null) { PopupMenu._cachPopup.IsOpen = false; } this.Popup = PopupManager.CreateOverlay(_popupRoot); this.Popup.Child = this; this.Popup.HorizontalOffset = aPos.X; this.Popup.VerticalOffset = aPos.Y; this.Popup.IsOpen = true; this.InvalidateArrange(); this.UpdateLayout(); GeometryHelper.EnsureControlInApplication(this.Popup, _popupRoot); PopupMenu._cachPopup = this.Popup; this.MoveLeaveMenuItem(); } public void Show(FrameworkElement relativeTo) { bool flag = PopupMenu._cachPopup != null; if (flag) { PopupMenu._cachPopup.IsOpen = false; } this.Popup = PopupManager.CreateOverlay(_popupRoot); PopupManager.MovePopup(this.Popup, relativeTo, PopupManager.RelativePositionHoriz.FlushLefts, PopupManager.RelativePositionVert.Below, _popupRoot); this.Popup.Child = this; this.Popup.IsOpen = true; this.InvalidateArrange(); this.UpdateLayout(); GeometryHelper.EnsureControlInApplication(this.Popup, _popupRoot); PopupMenu._cachPopup = this.Popup; this.MoveLeaveMenuItem(); } public void Show(MouseEventArgs aPos, Point offset) { if (PopupMenu._cachPopup != null) { PopupMenu._cachPopup.IsOpen = false; } this.Popup = PopupManager.CreateOverlay(aPos, _popupRoot); this.Popup.Child = this; this.Popup.IsOpen = true; this.InvalidateArrange(); this.UpdateLayout(); GeometryHelper.EnsureControlInApplication(this.Popup, _popupRoot); PopupMenu._cachPopup = this.Popup; this.MoveLeaveMenuItem(); } //public void ShowMainMenu(MainMenuButton MainMenuCtl) //{ // bool flag = PopupMenu._cachPopup != null; // if (flag) // { // PopupMenu._cachPopup.IsOpen = (false); // } // this.Popup = PopupManager.CreateAppOverlay(); // this.Popup.Child = this; // this.Popup.IsOpen = true; // this.Popup.InvalidateArrange(); // this.Popup.UpdateLayout(); // PopupManager.MovePopup(this.Popup, MainMenuCtl, PopupManager.RelativePositionHoriz.FlushLefts, PopupManager.RelativePositionVert.Below); // GeometryHelper.EnsureControlInApplication(this.Popup); // PopupMenu._cachPopup = this.Popup; // this.MoveLeaveMenuItem(); //} #endregion Public Methods #region Internal Methods internal void ClosePopup() { if (this.Popup != null) { this.Popup.IsOpen = false; } } #endregion Internal Methods #region Protected Methods protected virtual void OnPopupClosed() { var handler = PopupClosed; if (handler != null) handler(); } #endregion Protected Methods #region Private Methods private void MoveLeaveMenuItem() { var children = this.MainStack.Children; if (children == null || children.Count == 0) return; foreach (var child in children) { var current = child as ChartMenuItem; if (current == null) continue; current.HideHover(); } } private void PopupMenu_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { //中断事件冒泡, 单击左键不触发你容器事件 //主要是为了单击popup其它地方时关闭popup.- Popupmanager.cs: Root_MouseLeftButtonDown e.Handled = true; } private void Popup_Closed(object sender, EventArgs e) { OnPopupClosed(); var flag = (PopupMenu._cachPopup == this.Popup); if (flag) { PopupMenu._cachPopup = null; } this.Popup = null; } #endregion Private Methods #endregion Methods } }