Pārlūkot izejas kodu

Form - 右键菜单初始化、菜单command事件绑定

chenrongqian@kdanmobile.com 2 gadi atpakaļ
vecāks
revīzija
c72e03aeb1

+ 58 - 2
PDF Office/Helper/PopControlHelper.cs

@@ -222,17 +222,55 @@ namespace PDF_Office.Helper
     public class CustomPopMenu
     {
         public ContextMenu PopMenu { get; private set; }
+        public List<MenuItem> MenuItems { get; private set; }
+
         public CustomPopMenu(ContextMenu popMenu)
         {
             PopMenu = popMenu;
+            GetMenuItems();
+        }
+
+        private void GetMenuItems()
+        {
+            if (PopMenu == null || PopMenu.Items == null)
+                return;
+
+            MenuItems = new List<MenuItem>();
+
+            foreach (var item in PopMenu.Items)
+            {
+                if (item as MenuItem != null)
+                {
+                    MenuItems.Add(item as MenuItem);
+                }
+            }
+        }
+
+        //显示或隐藏所有菜单项
+        public void AllMenuVisibility(bool isVisual)
+        {
+            if (MenuItems == null) return;
+
+            foreach (var item in MenuItems)
+            {
+                item.Visibility = (isVisual ? Visibility.Visible : Visibility.Collapsed);
+            }
         }
 
         private MenuItem ContainterOfIndex(int index)
         {
-            if (PopMenu == null || PopMenu.Items.Count < index || index < 0)
+            if (MenuItems == null || MenuItems.Count < index || index < 0)
                 return null;
 
-            MenuItem menuItem = PopMenu.Items[index] as MenuItem;
+            MenuItem menuItem = MenuItems[index] as MenuItem;
+            return menuItem;
+        }
+
+        private MenuItem ContainterOfTag(string tag)
+        {
+            if (MenuItems == null ||  string.IsNullOrEmpty(tag))
+                return null;
+            var menuItem = MenuItems.FirstOrDefault(temp => temp.Tag.ToString() == tag);
             return menuItem;
         }
 
@@ -244,6 +282,24 @@ namespace PDF_Office.Helper
                 menuItem.Visibility = (isVisual?Visibility.Visible:Visibility.Collapsed);
             }
         }
+
+        public void SetVisibilityProperty(string tag, bool isVisual)
+        {
+            var menuItem = ContainterOfTag(tag);
+            if (menuItem != null)
+            {
+                menuItem.Visibility = (isVisual ? Visibility.Visible : Visibility.Collapsed);
+            }
+        }
+
+        public void SetTagProperty(int index, string tag)
+        {
+            var menuItem = ContainterOfIndex(index);
+            if (menuItem != null)
+            {
+                menuItem.Tag = tag;
+            }
+        }
         public void SetHeaderProperty(int index, string header)
         {
             var menuItem = ContainterOfIndex(index);

+ 214 - 1
PDF Office/ViewModels/Form/FormsToolContentViewModel.cs

@@ -12,6 +12,8 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Input;
 
 namespace PDF_Office.ViewModels.Form
 {
@@ -55,6 +57,7 @@ namespace PDF_Office.ViewModels.Form
         private IRegionManager regions;
 
         private string CurrentToolBtnType;
+        
         /// <summary>
         /// 按钮和属性面板键值对
         /// </summary>
@@ -72,6 +75,18 @@ namespace PDF_Office.ViewModels.Form
         public DelegateCommand ClosedMenuCommand { get; set; }
 
         public event EventHandler<string> UncheckedToolsBtnEvent;
+
+        #region 右键菜单
+        public DelegateCommand<object> PropertyMenuCommand { get; set; }
+        public DelegateCommand<object> CrossPageMenuCommand { get; set; }
+        public DelegateCommand<object> JumpPosMenuCommand { get; set; }
+        public DelegateCommand<object> HideNameMenuCommand { get; set; }
+        public DelegateCommand<object> SearchFormMenuCommand { get; set; }
+        public DelegateCommand<object> PrintMenuCommand { get; set; }
+        public DelegateCommand<object> DefaultValueMenuCommand { get; set; }
+        
+        #endregion
+
         #endregion
 
         #region 初始化
@@ -87,8 +102,59 @@ namespace PDF_Office.ViewModels.Form
             ClosedMenuCommand = new DelegateCommand(ClosedMenu);
             AlignmentClosedMenuCommand = new DelegateCommand(AlignmentClosedMenu);
             InitBtnToProperty();
+
+            //右键菜单
+            PropertyMenuCommand = new DelegateCommand<object>(PropertyMenu);
+            CrossPageMenuCommand = new DelegateCommand<object>(CrossPageMenu);
+            JumpPosMenuCommand = new DelegateCommand<object>(JumpPosMenu);
+            HideNameMenuCommand = new DelegateCommand<object>(HideNameMenu);
+            SearchFormMenuCommand = new DelegateCommand<object>(SearchFormMenu);
+            PrintMenuCommand = new DelegateCommand<object>(PrintMenu);
+            DefaultValueMenuCommand = new DelegateCommand<object>(DefaultValueMenu);
+        }
+
+        #region Command实现
+
+        #region Command右键菜单
+
+        private void PropertyMenu(object obj)
+        {
+
+        }
+        private void CrossPageMenu(object obj)
+        {
+
+        }
+
+        private void JumpPosMenu(object obj)
+        {
+
         }
 
+        private void HideNameMenu(object obj)
+        {
+
+        }
+
+        private void SearchFormMenu(object obj)
+        {
+
+        }
+
+        private void PrintMenu(object obj)
+        {
+
+        }
+
+        private void DefaultValueMenu(object obj)
+        {
+
+        }
+        
+
+        #endregion
+
+        #endregion
 
         private void InitBtnToProperty()
         {
@@ -343,6 +409,15 @@ namespace PDF_Office.ViewModels.Form
         {
             UncheckedToolsBtnEvent?.Invoke(null, CurrentToolBtnType);
             CurrentToolBtnType = "";
+            if (PDFViewer != null)
+            {
+                PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
+                PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
+                PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
+                PDFViewer.AnnotCommandHandler -= PDFViewer_WidgetCommandHandler;
+                PDFViewer.AnnotCommandHandler -= PDFViewer_WidgetCommandHandler;
+            }
+            
         }
 
         public void OnNavigatedTo(NavigationContext navigationContext)
@@ -363,9 +438,147 @@ namespace PDF_Office.ViewModels.Form
                 PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
                 PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
                 PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
+                PDFViewer.AnnotCommandHandler -= PDFViewer_WidgetCommandHandler;
+                PDFViewer.AnnotCommandHandler += PDFViewer_WidgetCommandHandler;
             }
         }
-        
+
+        private void PDFViewer_WidgetCommandHandler(object sender, AnnotCommandArgs e)
+        {
+            if (e == null)
+                return;
+
+            switch (e.CommandType)
+            {
+                case CommandType.Context:
+                    if(e.CommandTarget == TargetType.WidgetView)
+                    {
+                        WidgetViewCommandArgs widgetCommand = e as WidgetViewCommandArgs;
+                        if(widgetCommand != null && widgetCommand.AnnotEvent != null )
+                        {
+                            if (widgetCommand.AnnotEvent.AnnotItemsList.Count == 1)
+                            {
+                                e.PopupMenu = SelectedFormMenu(sender);
+                            }
+                            else if(widgetCommand.AnnotEvent.AnnotItemsList.Count > 1)
+                            {
+                                e.PopupMenu = MultiSelectedFormMenu(sender);
+                            }
+                         
+                        }
+                        
+                    }
+                    else
+                    {
+                        e.PopupMenu = BlankReaMenu(sender);
+                    }
+                    break;
+                default:
+                    e.DoCommand();
+                    break;
+
+            }
+            if (e.PopupMenu != null)
+            {
+                e.Handle = true;
+            }
+        }
+
+        private void InitMenu(CustomPopMenu customMenu,object sender)
+        {
+            int index= 0;
+            //属性
+            customMenu.SetMenuBinding(index, sender, PropertyMenuCommand);
+            customMenu.SetTagProperty(index, "Property");
+            index++;
+            //复制
+            customMenu.SetMenuBinding(index, sender, ApplicationCommands.Copy);
+            customMenu.SetTagProperty(index, "Copy");
+            index++;
+            //剪切
+            customMenu.SetMenuBinding(index, sender, ApplicationCommands.Cut);
+            customMenu.SetTagProperty(index, "Cut");
+            index++;
+            //粘贴
+            customMenu.SetMenuBinding(index, sender, ApplicationCommands.Paste);
+            customMenu.SetTagProperty(index, "Paste");
+            index++;
+            //删除
+            customMenu.SetMenuBinding(index, sender, ApplicationCommands.Delete);
+            customMenu.SetTagProperty(index, "Delete");
+            index++;
+            //创建多个副本
+            customMenu.SetMenuBinding(index, sender, ApplicationCommands.Delete);
+            customMenu.SetTagProperty(index, "CreateCopy");
+            index++;
+            //跨页复制
+            customMenu.SetMenuBinding(index, sender, CrossPageMenuCommand);
+            customMenu.SetTagProperty(index, "CrossPage");
+            index++;
+            //显示跳位编号
+            customMenu.SetMenuBinding(index, sender, JumpPosMenuCommand);
+            customMenu.SetTagProperty(index, "JumpPos");
+            index++;
+            //隐藏名称
+            customMenu.SetMenuBinding(index, sender, HideNameMenuCommand);
+            customMenu.SetTagProperty(index, "HideName");
+            index++;
+            //查找
+            customMenu.SetMenuBinding(index, sender, SearchFormMenuCommand);
+            customMenu.SetTagProperty(index, "SearchForm");
+            index++;
+            //打印
+            customMenu.SetMenuBinding(index, sender, PrintMenuCommand);
+            customMenu.SetTagProperty(index, "print");
+            index++;
+            //设置当前属性为默认值
+            customMenu.SetMenuBinding(index, sender, DefaultValueMenuCommand);
+            customMenu.SetTagProperty(index, "DefaultValue");
+            index++;
+            customMenu.AllMenuVisibility(true);
+           
+        }
+
+        //点击空白处
+        private ContextMenu BlankReaMenu(object sender)
+        {
+            var popMenu = App.Current.FindResource("FormContentMenu") as ContextMenu;
+            CustomPopMenu customMenu = new CustomPopMenu(popMenu);
+            InitMenu(customMenu, sender);
+            customMenu.SetVisibilityProperty("Property", false);
+            customMenu.SetVisibilityProperty("Copy", false);
+            customMenu.SetVisibilityProperty("Cut", false);
+            customMenu.SetVisibilityProperty("Delete", false);
+            customMenu.SetVisibilityProperty("CreateCopy", false);
+            customMenu.SetVisibilityProperty("CrossPage", false);
+            customMenu.SetVisibilityProperty("DefaultValue", false);
+            return popMenu;
+        }
+
+        //选中一个
+        private ContextMenu SelectedFormMenu(object sender)
+        {
+            var popMenu = App.Current.FindResource("FormContentMenu") as ContextMenu;
+            CustomPopMenu customMenu = new CustomPopMenu(popMenu);
+            InitMenu(customMenu, sender);
+            customMenu.SetVisibilityProperty("SearchForm", false);
+            customMenu.SetVisibilityProperty("print", false);
+            return popMenu;
+        }
+
+        //多选
+        private ContextMenu MultiSelectedFormMenu(object sender)
+        {
+            var popMenu = App.Current.FindResource("FormContentMenu") as ContextMenu;
+            CustomPopMenu customMenu = new CustomPopMenu(popMenu);
+            InitMenu(customMenu, sender);
+            customMenu.SetVisibilityProperty("CreateCopy", false);
+            customMenu.SetVisibilityProperty("DefaultValue", false);
+            customMenu.SetVisibilityProperty("SearchForm", false);
+            customMenu.SetVisibilityProperty("print", false);
+            return popMenu;
+        }
+
         #endregion
     }
 }

+ 2 - 2
PDF Office/ViewModels/Tools/AnnotToolContentViewModel.cs

@@ -737,9 +737,9 @@ namespace PDF_Office.ViewModels.Tools
 
         private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
         {
-            if (e.AnnotEventArgsList == null)
+            if (e.AnnotEventArgsList == null || (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool))
                 return;
-
+            
             // var annotlist = e.AnnotEventArgsList;
             switch (e.CommandType)
             {