Browse Source

PDFForm(Windows)-form undo/redo按钮可选状态

zhuyi 1 year ago
parent
commit
971a1108b3

+ 62 - 2
compdfkit_demo_windows/compdfkit/compdfkit-tools/Form/FormBarControl.xaml.cs

@@ -2,14 +2,19 @@
 using ComPDFKit.PDFAnnotation.Form;
 using ComPDFKit.PDFDocument.Action;
 using compdfkit_tools.Data;
+using compdfkit_tools.PDFControl;
 using ComPDFKitViewer.AnnotEvent;
 using ComPDFKitViewer.PdfViewer;
 using System;
 using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.ComponentModel;
 using System.Net.NetworkInformation;
+using System.Runtime.CompilerServices;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Controls.Primitives;
+using System.Windows.Data;
 using System.Windows.Documents.DocumentStructures;
 using System.Windows.Media;
 using System.Windows.Shapes;
@@ -20,7 +25,7 @@ namespace compdfkit_tools.Form
     /// <summary>
     /// FormBarControl.xaml 的交互逻辑
     /// </summary>
-    public partial class FormBarControl : UserControl
+    public partial class FormBarControl : UserControl, INotifyPropertyChanged
     {
         enum FromType
         {
@@ -51,6 +56,37 @@ namespace compdfkit_tools.Form
             {"Redo", "M16.7802 7.62131L17.3105 8.15164L16.7802 8.68197L13.1589 12.3033L12.0982 11.2426L14.4392 8.90164H7.74989C6.2311 8.90164 4.99989 10.1329 4.99989 11.6516C4.99989 13.1704 6.2311 14.4016 7.74989 14.4016H15.2499V15.9016H7.74989C5.40268 15.9016 3.49989 13.9988 3.49989 11.6516C3.49989 9.30443 5.40268 7.40164 7.74989 7.40164H14.4392L12.0982 5.06066L13.1589 4L16.7802 7.62131Z"},
         };
 
+        public event PropertyChangedEventHandler PropertyChanged;
+        protected void OnPropertyChanged([CallerMemberName] string name = null)
+        {
+            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
+        }
+
+
+        public bool CanUndo
+        {
+            get
+            {
+                if (pdfViewer != null)
+                {
+                    return pdfViewer.UndoManager.CanUndo;
+                }
+                return false;
+            }
+        }
+
+        public bool CanRedo
+        {
+            get
+            {
+                if (pdfViewer != null)
+                {
+                    return pdfViewer.UndoManager.CanRedo;
+                }
+                return false;
+            }
+        }
+
         private CPDFViewer pdfViewer;
 
         private FromPropertyControl fromPropertyControl = null;
@@ -149,9 +185,26 @@ namespace compdfkit_tools.Form
             button.Tag = Tag;
             button.Content = stackPanel;
             button.Click += UndoRedo_Click;
+            BingUndoRedoTag(button, Tag);
             FormGrid.Children.Add(button);
         }
 
+        private void BingUndoRedoTag(UIElement sender, string Tag)
+        {
+            Binding binding = new Binding();
+            binding.Source = this;
+            if (Tag == "Undo")
+            {
+                binding.Path = new System.Windows.PropertyPath("CanUndo");
+            }
+            else if (Tag == "Redo")
+            {
+                binding.Path = new System.Windows.PropertyPath("CanRedo");
+            }
+            binding.Mode = BindingMode.OneWay;
+            (sender as Button).SetBinding(Button.IsEnabledProperty, binding);
+        }
+
         private void CreateSeparator(double rotate)
         {
             RotateTransform rotateTransform = new RotateTransform();
@@ -173,9 +226,16 @@ namespace compdfkit_tools.Form
         public void SetPDFViewer(CPDFViewer pdfViewer, FromPropertyControl FromProperty)
         {
             this.pdfViewer = pdfViewer;
+            this.pdfViewer.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
+            this.pdfViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
             fromPropertyControl = FromProperty;
         }
 
+        private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
+        {
+            OnPropertyChanged(e.PropertyName);
+        }
+
         public void CheckedButtonForName(string name)
         {
             foreach (UIElement child in FormGrid.Children)
@@ -296,7 +356,7 @@ namespace compdfkit_tools.Form
                 fromPropertyControl.SetPropertyForType(null, null);
                 pdfViewer.UndoManager.Redo();
             }
-            
+
         }
 
         #endregion

+ 1 - 1
compdfkit_demo_windows/compdfkit/compdfkit-tools/Form/FromPropertyControl.xaml

@@ -7,7 +7,7 @@
              mc:Ignorable="d" 
              Loaded="UserControl_Loaded"
              Unloaded="UserControl_Unloaded"
-             d:DesignHeight="450" d:DesignWidth="800">
+             Width="260">
     <Grid >
         <Border x:Name="FromPropertyPanel"></Border>
     </Grid>

+ 1 - 1
compdfkit_demo_windows/compdfkit/compdfkit-tools/Form/Property/PushButtonProperty.xaml

@@ -125,8 +125,8 @@
                             <TextBlock Margin="0,20,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Text="Effect after selection" FontSize="14" FontFamily="Segoe UI" FontWeight="Bold"/>
                             <ComboBox x:Name="TextAlignmentCombox" Margin="0,8,0,0" Style="{StaticResource ComboBoxStyle1}" SelectedIndex="0" SelectionChanged="TextAlignmentCombox_SelectionChanged">
                                 <ComboBoxItem Content="None"/>
-                                <ComboBoxItem Content="Open a link"/>
                                 <ComboBoxItem Content="Jump to a link"/>
+                                <ComboBoxItem Content="Open a link"/>
                             </ComboBox>
                             <TextBox x:Name="ActionContentText" Margin="0,4,0,0" Text="123" TextChanged="ActionContentText_TextChanged" Visibility="Collapsed"/>
                         </StackPanel>

+ 10 - 6
compdfkit_demo_windows/compdfkit/compdfkit-tools/Form/Property/PushButtonProperty.xaml.cs

@@ -55,6 +55,7 @@ namespace compdfkit_tools.Form.Property
             SizeListbinding.Path = new System.Windows.PropertyPath("SizeList");
             FontSizeCombox.SetBinding(ComboBox.ItemsSourceProperty, SizeListbinding);
 
+            TopTabControl.SelectedIndex = 2;
             FieldNameText.Text = widgetArgs.FieldName;
             FormFieldCombox.SelectedIndex = (int)widgetArgs.FormField;
             BorderColorPickerControl.SetCheckedForColor(widgetArgs.LineColor);
@@ -63,7 +64,6 @@ namespace compdfkit_tools.Form.Property
             SetFontName(widgetArgs.FontName);
             SetFontStyle(widgetArgs.IsItalic, widgetArgs.IsBold);
             SetFontSize(widgetArgs.FontSize);
-            TopTabControl.SelectedIndex = 2;
             ItemText.Text = widgetArgs.Text;
             SetActionContext(widgetArgs.ActionDict);
             IsLoadedData = true;
@@ -80,13 +80,15 @@ namespace compdfkit_tools.Form.Property
             {
                 if (key == C_ACTION_TYPE.ACTION_TYPE_GOTO)
                 {
-                    TextAlignmentCombox.SelectedIndex = 1;
-                    ActionContentText.Text = keyValuePairs[key];
+                    TextAlignmentCombox.SelectedIndex = 1; 
+                    ActionContentText.Visibility = Visibility.Visible;
+                    ActionContentText.Text = (Convert.ToInt32( keyValuePairs[key])+1).ToString();
                     break;
                 }
                 if (key == C_ACTION_TYPE.ACTION_TYPE_URI)
                 {
-                    TextAlignmentCombox.SelectedIndex = 2;
+                    TextAlignmentCombox.SelectedIndex = 2; 
+                    ActionContentText.Visibility = Visibility.Visible;
                     ActionContentText.Text = keyValuePairs[key];
                     break;
                 }
@@ -249,15 +251,17 @@ namespace compdfkit_tools.Form.Property
                         break;
                     case 1:
                         ActionContentText.Visibility = Visibility.Visible;
+                        ActionContentText.Text = "";
                         break;
                     case 2:
                         ActionContentText.Visibility = Visibility.Visible;
                         ActionContentText.Style = this.FindResource("txtboxStyle") as Style;
+                        ActionContentText.Text = "";
                         break;
                     default:
                         break;
                 }
-                if (ActionContentText.Visibility != Visibility.Collapsed && ActionContentText != null && !string.IsNullOrEmpty(ActionContentText.Text))
+                if (ActionContentText.Visibility != Visibility.Collapsed && ActionContentText != null)
                     AddAction();
             }
         }
@@ -273,7 +277,7 @@ namespace compdfkit_tools.Form.Property
                 if (page - 1 >= 0)
                     ActionDict[C_ACTION_TYPE.ACTION_TYPE_GOTO] = (page - 1).ToString();
             }
-            if (TextAlignmentCombox.SelectedIndex == 2 && !string.IsNullOrEmpty(ActionContentText.Text))
+            if (TextAlignmentCombox.SelectedIndex == 2)
             {
                 if (string.IsNullOrEmpty(ActionContentText.Text.Trim()))
                 {

+ 5 - 3
compdfkit_demo_windows/compdfkit/form-ctrl-demo/MainWindow.xaml.cs

@@ -615,7 +615,6 @@ namespace form_ctrl_demo
         }
         public void ExpandRightPropertyPanel(UIElement properytPanel, Visibility visible)
         {
-            PropertyContainer.Width = 260;
             PropertyContainer.Child = properytPanel;
             PropertyContainer.Visibility = visible;
         }
@@ -788,7 +787,6 @@ namespace form_ctrl_demo
                     if (PropertyContainer != null)
                     {
                         fromPropertyControl.CleanProperty();
-                        PropertyContainer.Width = 0;
                         PropertyContainer.Child = null;
                         ViewSettingBtn.IsChecked = false;
                         FormBarBtn.IsChecked = false;
@@ -805,7 +803,6 @@ namespace form_ctrl_demo
                     if (PropertyContainer != null)
                     {
                         fromPropertyControl.CleanProperty();
-                        PropertyContainer.Width = 0;
                         PropertyContainer.Child = null;
                         ViewSettingBtn.IsChecked = false;
                         FormBarBtn.IsChecked = false;
@@ -832,6 +829,11 @@ namespace form_ctrl_demo
                 ExpandRightPropertyPanel(fromPropertyControl, Visibility.Visible);
                 fromPropertyControl.SetPropertyForType(e, null);
             }
+            else
+            {
+                FormBarBtn.IsChecked = false;
+                ExpandRightPropertyPanel(null, Visibility.Collapsed);
+            }
         }
 
         private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)