internal void OpenSubMenu() { if (Items.Count == 0 || IsSubmenuOpen) { return; } if (ParentMi != null) { // 关闭同级已打开的菜单 if (Kit.IsPhoneUI && ParentMi.IsSubmenuOpen) { // phone模式关闭上级窗口 ParentMi.IsSubmenuOpen = false; } else { Mi last = ParentMi._lastSelected; if (last != null && last != this) { last.CloseSubMenu(); last.IsSelected = false; } } ParentMi._lastSelected = this; } else if (Kit.IsPhoneUI && Owner.IsContextMenu) { Owner.Close(); } IsSelected = true; IsSubmenuOpen = true; }
protected override void OnApplyCellTemplate() { var grid = (Grid)GetTemplateChild("Grid"); grid.Children.Add(_fl); var btn = (Button)GetTemplateChild("BtnAdd"); if (btn != null) { if (Type.GetType(FileItem.SelectFileDlgType) != null) { var menu = new Menu { Placement = MenuPosition.BottomLeft }; Mi mi = new Mi { ID = "上传图像", Icon = Icons.曲别针 }; mi.Click += (s, e) => OnAddImage(); menu.Items.Add(mi); mi = new Mi { ID = "选择图像", Icon = Icons.图片 }; mi.Click += OnSelectFiles; menu.Items.Add(mi); Ex.SetMenu(btn, menu); } else { btn.Click += (s, e) => OnAddImage(); } } UpdateDefaultMenu(); }
static void OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Mi item = (Mi)d; bool newValue = (bool)e.NewValue; if (!newValue) { item.IsMouseOver = false; item.ChangeState(MenuItemState.Normal); } else { item.ChangeState(MenuItemState.Pressed); } if ((bool)e.OldValue) { if (item.IsSubmenuOpen) { item.CloseSubMenu(); } } // 冒泡处理,只处理转为选择状态的 if (newValue) { item.Owner?.OnItemIsSelected(item); } }
/// <summary> /// 选择某个菜单项时的处理 /// </summary> /// <param name="p_mi"></param> internal void OnItemIsSelected(Mi p_mi) { if (_isBubbling) { return; } // 冒泡处理 _isBubbling = true; Mi child = p_mi; Mi par = p_mi.ParentMi; while (par != null) { par.OnChildIsSelected(child); child = par; par = par.ParentMi; } // 新选择的为一级菜单项 if (SelectedMi != p_mi && p_mi.ParentMi == null) { // 关闭上一菜单项的子项窗口 if (SelectedMi != null) { SelectedMi.CloseSubMenu(); } SelectedMi = p_mi; } _isBubbling = false; }
/// <summary> /// 选择改变事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void OnSelectionChanged(object sender, EventArgs e) { if (!Info.Root.ViewSetting.ShowContextMenu) { return; } _selectedTable = null; _excel.DecorationRange = null; Worksheet sheet = _excel.ActiveSheet; if (sheet == null || sheet.Selections.Count != 1) { return; } CellRange range = sheet.Selections[0]; SheetTable[] st = sheet.GetTables(); foreach (SheetTable tbl in st) { if (tbl.Range.Intersects(range.Row, range.Column, range.RowCount, range.ColumnCount)) { _selectedTable = tbl; _excel.DecorationRange = tbl.Range; break; } } // 选择区包含表格 单行时不显示菜单 if (_selectedTable != null || range.RowCount == 1) { return; } if (_selectionMenu == null) { _selectionMenu = new Menu { IsContextMenu = true }; Mi mi = new Mi { ID = "转为表格", Icon = Icons.田字格 }; mi.Click += (s, args) => AddSheetTable(_excel.ActiveSheet.Selections[0]); _selectionMenu.Items.Add(mi); mi = new Mi { ID = "生成柱状图", Icon = Icons.对比图 }; mi.Click += (s, args) => AddChart(_excel.ActiveSheet.Selections[0]); _selectionMenu.Items.Add(mi); } Point topLeft = _excel.GetAbsolutePosition(); Rect rc = _excel.ActiveSheet.GetRangeBound(range); double x = topLeft.X + rc.X + rc.Width + 5 - (_excel.ActiveSheet.RowHeader.IsVisible ? 0 : _excel.ActiveSheet.RowHeader.DefaultColumnWidth); double y = topLeft.Y + rc.Y - (_excel.ActiveSheet.ColumnHeader.IsVisible ? 0 : _excel.ActiveSheet.ColumnHeader.DefaultRowHeight); _ = _selectionMenu.OpenContextMenu(new Point(x, y)); }
void OpenOrCloseSubMenu() { if (IsSubmenuOpen) { IsSelected = true; SelectedMi = null; // 打开子菜单窗口 if (_dlg == null) { _dlg = new SubMenuDlg(this); } _dlg.ShowDlg(); } else { if (SelectedMi != null && SelectedMi.IsSubmenuOpen) { SelectedMi.CloseSubMenu(); } SelectedMi = null; if (!IsMouseOver) { ChangeState(MenuItemState.Normal); } // 关闭子菜单窗口 if (_dlg != null) { _dlg.Close(); } } }
void InitOwner(Menu p_owner, Mi p_parent) { Owner = p_owner; ParentMi = p_parent; InitSubsOwner(); Items.ItemsChanged -= OnItemsChanged; Items.ItemsChanged += OnItemsChanged; }
/// <summary> /// 关闭菜单 /// </summary> public void Close() { SelectedMi = null; // 上下文菜单 if (_dlg != null && _dlg.IsOpened) { _dlg.Close(); } }
static void OnShowInPhoneChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Mi mi = (Mi)d; if (mi._loaded && mi.ParentMi == null) { mi.UpdateRoleState(); } }
async void OnSelectFiles(object sender, Mi e) { var dlg = (ISelectFileDlg)Activator.CreateInstance(Type.GetType(FileItem.SelectFileDlgType)); if (await dlg.Show(MaxFileCount > 1, null) && dlg.SelectedFiles != null && dlg.SelectedFiles.Count > 0) { _fl.AddExistFiles(dlg.SelectedFiles); } }
void OnExcelRightTapped(object sender, RightTappedRoutedEventArgs e) { if (!Info.Root.ViewSetting.ShowContextMenu || _excel.ActiveSheet == null) { return; } if (_rightMenu == null) { _rightMenu = new Menu { IsContextMenu = true }; Mi mi = new Mi { ID = "删除表格", Icon = Icons.田字格 }; mi.Click += (s, args) => DelSheetTable(); _rightMenu.Items.Add(mi); mi = new Mi { ID = "清除所有图表", Icon = Icons.对比图 }; mi.Click += (s, args) => ClearChart(); _rightMenu.Items.Add(mi); mi = new Mi { ID = "清除所有表格", Icon = Icons.田字格 }; mi.Click += (s, args) => ClearTable(); _rightMenu.Items.Add(mi); _rightMenu.Items.Add(new Mi { ID = "导出", Icon = Icons.导出, Cmd = CmdExport }); _rightMenu.Items.Add(new Mi { ID = "打印", Icon = Icons.打印, Cmd = CmdPrint }); _rightMenu.Items.Add(new Mi { ID = "显示网格", IsCheckable = true, IsChecked = _excel.ActiveSheet.ShowGridLine, Cmd = CmdGridLine }); _rightMenu.Items.Add(new Mi { ID = "显示列头", IsCheckable = true, IsChecked = _excel.ActiveSheet.ColumnHeader.IsVisible, Cmd = CmdColHeader }); _rightMenu.Items.Add(new Mi { ID = "显示行头", IsCheckable = true, IsChecked = _excel.ActiveSheet.RowHeader.IsVisible, Cmd = CmdRowHeader }); } _rightMenu["删除表格"].Visibility = _selectedTable != null ? Visibility.Visible : Visibility.Collapsed; _rightMenu["清除所有图表"].Visibility = _excel.ActiveSheet.Charts.Count > 0 ? Visibility.Visible : Visibility.Collapsed; _rightMenu["清除所有表格"].Visibility = _excel.ActiveSheet.GetTables().Length > 0 ? Visibility.Visible : Visibility.Collapsed; // 脚本处理上下文菜单 Info.ScriptObj?.OpenContextMenu(_rightMenu); _ = _rightMenu.OpenContextMenu(e.GetPosition(null)); }
/// <summary> /// 递归关闭所有子窗口 /// </summary> internal void CloseSubMenu() { if (_lastSelected != null) { _lastSelected.CloseSubMenu(); _lastSelected = null; } if (IsSubmenuOpen) { IsSubmenuOpen = false; } }
async void OnSelectFiles(object sender, Mi e) { var dlg = (ISelectFileDlg)Activator.CreateInstance(Type.GetType(FileItem.SelectFileDlgType)); if (await dlg.Show(false, FileItem.ImageExt) && dlg.SelectedFiles != null && dlg.SelectedFiles.Count > 0) { _fl.AddExistFiles(new List <string> { dlg.SelectedFiles[0] }); } }
static void OnScopeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Mi mi = (Mi)d; if (Kit.IsPhoneUI) { mi.Visibility = ((MiScope)e.NewValue) == MiScope.Windows ? Visibility.Collapsed : Visibility.Visible; } else { mi.Visibility = ((MiScope)e.NewValue) == MiScope.Phone ? Visibility.Collapsed : Visibility.Visible; } }
internal void UpdateOwner(Menu p_owner, Mi p_parent) { Owner = p_owner; ParentMi = p_parent; if (_loaded) { UpdateRoleState(); } else { InitSubsOwner(); Items.ItemsChanged -= OnItemsChanged; Items.ItemsChanged += OnItemsChanged; } }
void ShowMenu(Point p_pos) { if (_menu == null) { _menu = new Menu { IsContextMenu = true }; var item = new Mi { ID = "系统监控" }; item.Click += (s, a) => SysTrace.ShowBox(); _menu.Items.Add(item); } _ = _menu.OpenContextMenu(p_pos); }
void LoadDefaultToolbar() { var bar = Toolbar; // 外部自定义 if (bar != null && bar != _toolbar) { return; } // 不显示 或 只读 if (!ShowDefaultToolbar || ReadOnlyBinding) { ClearValue(ToolbarProperty); return; } if (_toolbar == null) { Button btn = new Button { Content = "\uE038", Style = Res.字符按钮 }; if (Type.GetType(FileItem.SelectFileDlgType) != null) { var menu = new Menu { Placement = MenuPosition.BottomLeft }; Mi mi = new Mi { ID = "上传文件", Icon = Icons.曲别针 }; mi.Click += (s, e) => OnAddFile(); menu.Items.Add(mi); mi = new Mi { ID = "选择文件", Icon = Icons.文件夹 }; mi.Click += OnSelectFiles; menu.Items.Add(mi); Ex.SetMenu(btn, menu); } else { btn.Click += (s, e) => OnAddFile(); } _toolbar = btn; } Toolbar = _toolbar; }
/// <summary> /// Phone模式附加标题右键菜单 /// </summary> /// <param name="p_elem">标题元素</param> /// <param name="p_win">所属窗口</param> public static void OnPhoneTitleTapped(FrameworkElement p_elem, Win p_win) { if (p_elem == null || p_win == null) { return; } p_elem.RightTapped += (s, e) => { if (_menu == null) { _menu = new Menu { IsContextMenu = true, Placement = MenuPosition.BottomLeft }; var item = new Mi { ID = "取消自启动" }; item.Click += (o, a) => Startup.DelAutoStart(); _menu.Items.Add(item); item = new Mi { ID = "设置自启动" }; item.Click += SetAutoStart; _menu.Items.Add(item); item = new Mi { ID = "系统监视" }; item.Click += (o, a) => SysTrace.ShowBox(); _menu.Items.Add(item); } var autoStart = AtState.GetAutoStart(); if (autoStart != null && autoStart.WinType == p_win.GetType().AssemblyQualifiedName && (p_win.Params == null || autoStart.Params == JsonSerializer.Serialize(p_win.Params, JsonOptions.UnsafeSerializer))) { _menu.Items[0].Visibility = Visibility.Visible; _menu.Items[1].Visibility = Visibility.Collapsed; } else { _menu.Items[0].Visibility = Visibility.Collapsed; _menu.Items[1].Visibility = Visibility.Visible; _currentWin = p_win; } _ = _menu.OpenContextMenu(default, p_elem);
void OnItemsChanged(object sender, ItemListChangedArgs e) { if (e.CollectionChange == CollectionChange.ItemInserted) { Mi mi = ((MiList)sender)[e.Index]; mi.InitOwner(Owner, this); } else if (e.CollectionChange == CollectionChange.Reset) { InitSubsOwner(); } if (_loaded) { UpdateRoleState(); } }
static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Mi mi = (Mi)d; if (mi.IsCheckable) { mi.UpdateCheckedIcon(); if ((bool)e.NewValue) { mi.Checked?.Invoke(mi, EventArgs.Empty); } else { mi.Unchecked?.Invoke(mi, EventArgs.Empty); } } }
void UpdateDefaultMenu() { if (Ex.GetMenu(this) != null || !ShowDefaultMenu) { return; } Menu menu = new Menu(); Mi mi = new Mi { ID = "分享", Icon = Icons.分享 }; mi.SetBinding(Mi.CmdProperty, new Binding { Path = new PropertyPath("CmdShare") }); menu.Items.Add(mi); mi = new Mi { ID = "保存", Icon = Icons.保存 }; mi.SetBinding(Mi.CmdProperty, new Binding { Path = new PropertyPath("CmdSaveAs") }); menu.Items.Add(mi); if (!ReadOnlyBinding) { mi = new Mi { ID = "更新", Icon = Icons.刷新 }; mi.SetBinding(Mi.CmdProperty, new Binding { Path = new PropertyPath("CmdUpdate") }); menu.Items.Add(mi); mi = new Mi { ID = "删除", Icon = Icons.除 }; mi.SetBinding(Mi.CmdProperty, new Binding { Path = new PropertyPath("CmdDelete") }); menu.Items.Add(mi); } Ex.SetMenu(_fl, menu); }
internal void OnChildIsSelected(Mi p_mi) { // 子菜单项为选择状态 if (SelectedMi != p_mi) { if (SelectedMi != null) { SelectedMi.CloseSubMenu(); SelectedMi.IsSelected = false; } SelectedMi = p_mi; if (!IsSelected) { IsSelected = true; } } }
static void ShowMenu(Point p_pos) { if (_menu == null) { _menu = new Menu { IsContextMenu = true }; var item = new Mi { ID = "取消自启动" }; item.Click += (s, a) => Startup.DelAutoStart(); _menu.Items.Add(item); item = new Mi { ID = "设置自启动" }; item.Click += SetAutoStart; _menu.Items.Add(item); item = new Mi { ID = "恢复窗口默认布局" }; item.Click += ResetWinLayout; _menu.Items.Add(item); item = new Mi { ID = "关闭其他" }; item.Click += CloseOtherWin; _menu.Items.Add(item); item = new Mi { ID = "停靠在左侧" }; item.Click += DockLeft; _menu.Items.Add(item); item = new Mi { ID = "停靠在右侧" }; item.Click += DockRight; _menu.Items.Add(item); } var autoStart = AtState.GetAutoStart(); Win win = _currentItem._win; if (autoStart != null && win != null && autoStart.WinType == win.GetType().AssemblyQualifiedName && (win.Params == null || autoStart.Params == JsonSerializer.Serialize(win.Params, JsonOptions.UnsafeSerializer))) { _menu.Items[0].Visibility = Visibility.Visible; _menu.Items[1].Visibility = Visibility.Collapsed; } else { _menu.Items[0].Visibility = Visibility.Collapsed; _menu.Items[1].Visibility = Visibility.Visible; } if (win != null) { if (win.AllowResetLayout) { _menu.Items[2].Visibility = Visibility.Visible; } else { // 主区内容为Win Tabs tabs = (Tabs)win.GetValue(Win.MainTabsProperty); if (tabs != null && tabs.Items.Count > 0 && tabs.Items[0].Content is Win cw && cw.AllowResetLayout) { _menu.Items[2].Visibility = Visibility.Visible; } else { _menu.Items[2].Visibility = Visibility.Collapsed; } }
/// <summary> /// 设置Mi要绑定的Lv /// </summary> public static void SetLvMultiSelect(this Mi d, Lv value) { d.SetValue(LvMultiSelectProperty, value); }
internal void OnItemClick(Mi p_item) { ItemClick?.Invoke(this, p_item); }
/// <summary> /// 获取Mi要绑定的Lv /// </summary> public static Lv GetLvMultiSelect(this Mi d) { return((Lv)d.GetValue(LvMultiSelectProperty)); }