Browse Source

注释列表-删除后刷新数据

OYXH\oyxh 1 year ago
parent
commit
93621b83e3
1 changed files with 69 additions and 41 deletions
  1. 69 41
      PDF Office/ViewModels/BOTA/AnnotationContentViewModel.cs

+ 69 - 41
PDF Office/ViewModels/BOTA/AnnotationContentViewModel.cs

@@ -89,7 +89,6 @@ namespace PDF_Master.ViewModels.BOTA
             }
         }
 
-
         private string T_collapse;
 
         public string T_Collapse
@@ -275,8 +274,7 @@ namespace PDF_Master.ViewModels.BOTA
                 {
                     return false;
                 }
-            }
-            
+            }
         }
 
         private List<string> colors = new List<string>();
@@ -398,7 +396,7 @@ namespace PDF_Master.ViewModels.BOTA
                 catch (Exception ex)
                 {
                     AlertsMessage alertsMessage = new AlertsMessage();
-                    alertsMessage.ShowDialog(App.MainPageLoader.GetString("Dialog_Hint"), App.MainPageLoader.GetString("Annot_ExportFailWarning")+ex.Message, App.ServiceLoader.GetString("Text_ok"));
+                    alertsMessage.ShowDialog(App.MainPageLoader.GetString("Dialog_Hint"), App.MainPageLoader.GetString("Annot_ExportFailWarning") + ex.Message, App.ServiceLoader.GetString("Text_ok"));
                 }
             }
         }
@@ -1043,7 +1041,7 @@ namespace PDF_Master.ViewModels.BOTA
             //var list = PdfViewer.GetAnnotCommentList(pageindex, PdfViewer.Document);
             for (int i = 0; i < AnnotationList.Items.Count; i++)
             {
-                if(AnnotationList.Items[i] is AnnotationHandlerEventArgs item)
+                if (AnnotationList.Items[i] is AnnotationHandlerEventArgs item)
                 {
                     if (item.AnnotIndex == annotindex && item.PageIndex == pageindex)
                     {
@@ -1062,7 +1060,6 @@ namespace PDF_Master.ViewModels.BOTA
                         }
                     }
                 }
-                
             }
         }
 
@@ -1147,37 +1144,66 @@ namespace PDF_Master.ViewModels.BOTA
         public void DeleteModifiedAnnot(int pageIndex, int annotIndex)
         {
             var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
-            //if (items.Count == 0)
-            //{
-            //    for (int k = 0; k < AnnotationListItems.Count; k++)
-            //    {
-            //        if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
-            //        {
-            //            AnnotationListItems.RemoveAt(k);
-            //            AnnotationHandlerEventArgs annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
-            //            CurrentAnnotationLists.Remove(annotation1);
-            //            UpdateAnnotListAfterDelete(pageIndex, annotIndex);
-            //            return;
-            //        }
-            //    }
-            //}
-            //else
-            //{
-            for (int k = 0; k < AnnotationListItems.Count; k++)
+            if (items.Count == 0)
             {
-                //不能加item[j].annoteIndex = annotIndex  删除后必匹配不上
-                if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
+                for (int k = 0; k < AnnotationListItems.Count; k++)
                 {
-                    AnnotationHandlerEventArgs annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
-                    CurrentAnnotationLists.Remove(annotation1);
-                    AnnotationListItems.RemoveAt(k);
-                    UpdateAnnotListAfterDelete(pageIndex, annotIndex);
-                    return;
+                    if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
+                    {
+                        AnnotationListItems.RemoveAt(k);
+                        AnnotationHandlerEventArgs annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
+                        CurrentAnnotationLists.Remove(annotation1);
+                        UpdateAnnotListAfterDelete(pageIndex, annotIndex);
+
+                        //删除之后要拿到最新的数据源
+                        CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
+                        AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
+                        AnnotationListItems.AddRange(CurrentAnnotationLists);
+                        AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
+                        RefreshAnnotationListItems();
+                        AnnotationHandlerEventArgs args = AnnotationListItems.FindFirst(a => a.PageIndex == pageIndex);
+                        listBox.ScrollIntoView(args);
+                        return;
+                    }
                 }
             }
-            //}
-        }
-
+            else
+            {
+                for (int k = 0; k < AnnotationListItems.Count; k++)
+                {
+                    //不能加item[j].annoteIndex = annotIndex  删除后必匹配不上
+                    if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
+                    {
+                        AnnotationHandlerEventArgs annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
+                        CurrentAnnotationLists.Remove(annotation1);
+                        AnnotationListItems.RemoveAt(k);
+                        UpdateAnnotListAfterDelete(pageIndex, annotIndex);
+
+                        //删除之后要拿到最新的数据源
+                        RefreshAnnotation(pageIndex);
+
+                        return;
+                    }
+                }
+            }
+        }
+
+        private async void RefreshAnnotation(int pageIndex)
+        {
+            CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
+            AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
+            AnnotationListItems.AddRange(CurrentAnnotationLists);
+            AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
+            //RefreshAnnotationListItems();
+            PageSortEvent();
+            AnnotationHandlerEventArgs args = AnnotationListItems.FindFirst(a => a.PageIndex == pageIndex);
+            await Task.Delay(5);//不加延时 每页的第一个注释不会展开
+            ExpandGroupHeader(args, listBox);
+
+            await Task.Delay(5);//不加延时 会有不滚动的现象
+            listBox.ScrollIntoView(args);
+        }
+
         public async void UpdateModifiedAnnot(int pageIndex, int annotIndex/*, bool isDelete*/)
         {
             try
@@ -1243,12 +1269,11 @@ namespace PDF_Master.ViewModels.BOTA
                             {
                                 if (listBox.SelectedItems.IndexOf(AnnotationListItems[k]) == -1)
                                 //if (!listBox.SelectedItems.Contains(AnnotationListItems[k]))
-                                {
-
+                                {
                                     IsModified = false;
-                                    listBox.SelectedItem = AnnotationListItems[k];
-                                    
-                                    //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
+                                    listBox.SelectedItem = AnnotationListItems[k];
+
+                                    //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
                                 }
                             }
 
@@ -1268,7 +1293,7 @@ namespace PDF_Master.ViewModels.BOTA
         /// <summary>
         /// 展开item所在分组
         /// </summary>
-        private void ExpandGroupHeader(AnnotationHandlerEventArgs item, ListBox listBox)
+        private async void ExpandGroupHeader(AnnotationHandlerEventArgs item, ListBox listBox)
         {
             try
             {
@@ -1285,8 +1310,11 @@ namespace PDF_Master.ViewModels.BOTA
                             int count = VisualTreeHelper.GetChildrenCount(stackpanel);
                             var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
                             var g = CommonHelper.FindVisualChild<Expander>(groupItem);
-                            if (g != null)
-                                g.IsExpanded = true;
+                            if (g != null)
+                            {
+                                await Task.Delay(5);
+                                g.IsExpanded = true;
+                            }
                         }
                     }
                 }