Kaynağa Gözat

Added a way to change a tab name in the OnRename event. Fixed Collapse Headers. Worked on ContextMenu utils.

Kenric Nugteren 2 yıl önce
ebeveyn
işleme
29b8b1bfa8

+ 6 - 2
InABox.DynamicGrid/DynamicTabControl.cs

@@ -153,6 +153,9 @@ namespace InABox.DynamicGrid
             set => SetValue(CanRenameProperty, value);
         }
         
+        /// <summary>
+        /// Called when a tab is renamed. Note that changing <see cref="DynamicTabItemRenamedEventArgs.NewName"/> will change the new name of the tab.
+        /// </summary>
         public event DynamicTabItemRenamedEvent OnTabRenamed;
         
         static DynamicTabItem()
@@ -200,8 +203,9 @@ namespace InABox.DynamicGrid
                             String tabname = Header.ToString() ?? string.Empty;
                             if (TextEdit.Execute("Rename Tab", ref tabname))
                             {
-                                OnTabRenamed?.Invoke(this, new DynamicTabItemRenamedEventArgs() { OldName = Header.ToString() ?? string.Empty, NewName = tabname });
-                                Header = tabname;
+                                var args = new DynamicTabItemRenamedEventArgs() { OldName = Header.ToString() ?? string.Empty, NewName = tabname };
+                                OnTabRenamed?.Invoke(this, args);
+                                Header = args.NewName;
                                 var parent = Parent as DynamicTabControl;
                                 if (parent != null)
                                     parent.ChangedCommand.Execute(null);

+ 4 - 4
InABox.DynamicGrid/FormDesigner/DynamicFormDesignGrid.cs

@@ -362,7 +362,7 @@ namespace InABox.DynamicGrid
         private static void AddClick<TTag>(ButtonBase button, TTag tag, Action<TTag> click)
         {
             button.Tag = tag;
-            button.Click += (o, e) => click((TTag)(o as MenuItem)!.Tag);
+            button.Click += (o, e) => click((TTag)(o as FrameworkElement)!.Tag);
         }
 
         #endregion
@@ -961,11 +961,11 @@ namespace InABox.DynamicGrid
         {
             if (!IsDesigning)
             {
-                foreach (var header in elementmap.Where(x => x.Value is FormHeader head).Select(x => x.Value).Cast<FormHeader>())
+                foreach (var header in elementmap.Where(x => x.Value is DFHeaderControl head).Select(x => x.Value).Cast<DFHeaderControl>())
                 {
-                    if (header.Collapsed)
+                    if (header.Header.Collapsed)
                     {
-                        CollapseRows(header, true);
+                        CollapseRows(header.Header, true);
                     }
                 }
             }

+ 42 - 23
inabox.wpf/WPFUtils.cs

@@ -80,19 +80,33 @@ namespace InABox.WPF
                 }
         }
 
-        private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, bool enabled)
+        #region Menu Utils
+
+        private static void ItemsControlInsert(ItemsControl menu, FrameworkElement item, int index)
+        {
+            if (index != -1)
+            {
+                menu.Items.Insert(index, item);
+            }
+            else
+            {
+                menu.Items.Add(item);
+            }
+        }
+
+        private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, bool enabled, int index = -1)
         {
             var item = new MenuItem { Header = caption, IsEnabled = enabled };
             if (image != null)
                 item.Icon = new Image() { Source = enabled ? image.AsBitmapImage(24, 24) : image.AsGrayScale().AsBitmapImage(24, 24) };
 
-            menu.Items.Add(item);
+            ItemsControlInsert(menu, item, index);
             return item;
         }
 
-        private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, Action? click, bool enabled)
+        private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, Action? click, bool enabled, int index = -1)
         {
-            var item = DoAddMenuItem(menu, caption, image, enabled);
+            var item = DoAddMenuItem(menu, caption, image, enabled, index);
 
             if (click != null)
             {
@@ -104,9 +118,9 @@ namespace InABox.WPF
 
             return item;
         }
-        private static MenuItem DoAddMenuItem<T>(ItemsControl menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled)
+        private static MenuItem DoAddMenuItem<T>(ItemsControl menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled, int index = -1)
         {
-            var item = DoAddMenuItem(menu, caption, image, enabled);
+            var item = DoAddMenuItem(menu, caption, image, enabled, index);
 
             item.Tag = tag;
             item.Click += (o, e) =>
@@ -119,7 +133,7 @@ namespace InABox.WPF
 
         public delegate void CheckToggleAction<T>(T tag, bool isChecked);
 
-        private static MenuItem DoAddCheckItem<T>(ItemsControl menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked, bool enabled)
+        private static MenuItem DoAddCheckItem<T>(ItemsControl menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked, bool enabled, int index = -1)
         {
             var item = new MenuItem { Header = caption, IsEnabled = enabled, IsCheckable = true, IsChecked = isChecked };
 
@@ -129,41 +143,46 @@ namespace InABox.WPF
                 click((T)(o as MenuItem)!.Tag, item.IsChecked);
             };
 
-            menu.Items.Add(item);
+            ItemsControlInsert(menu, item, index);
             return item;
         }
 
-        public static Separator AddSeparator(this ContextMenu menu)
+        public static Separator AddSeparator(this ContextMenu menu, int index = -1)
         {
             var separator = new Separator();
 
-            menu.Items.Add(separator);
+            ItemsControlInsert(menu, separator, index);
 
             return separator;
         }
-        public static Separator? AddSeparatorIfNeeded(this ContextMenu menu)
+        public static Separator? AddSeparatorIfNeeded(this ContextMenu menu, int index = -1)
         {
             if (menu.Items.Count == 0) return null;
 
-            var last = menu.Items[menu.Items.Count - 1];
+            var lastIndex = index != -1 ? index - 1 : menu.Items.Count - 1;
+            if (lastIndex < 0 || lastIndex >= menu.Items.Count) return null;
+
+            var last = menu.Items[lastIndex];
             if (last is Separator) return null;
 
             var separator = new Separator();
 
-            menu.Items.Add(separator);
+            ItemsControlInsert(menu, separator, index);
 
             return separator;
         }
 
-        public static MenuItem AddItem(this ContextMenu menu, string caption, Bitmap? image, Action? click, bool enabled = true)
-            => DoAddMenuItem(menu, caption, image, click, enabled);
-        public static MenuItem AddItem(this MenuItem menu, string caption, Bitmap? image, Action? click, bool enabled = true)
-            => DoAddMenuItem(menu, caption, image, click, enabled);
-        public static MenuItem AddItem<T>(this ContextMenu menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true)
-            => DoAddMenuItem(menu, caption, image, tag, click, enabled);
-        public static MenuItem AddItem<T>(this MenuItem menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true)
-            => DoAddMenuItem(menu, caption, image, tag, click, enabled);
-        public static MenuItem AddCheckItem<T>(this ContextMenu menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked = false, bool enabled = true)
-            => DoAddCheckItem<T>(menu, caption, tag, click, isChecked, enabled);
+        public static MenuItem AddItem(this ContextMenu menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
+            => DoAddMenuItem(menu, caption, image, click, enabled, index);
+        public static MenuItem AddItem(this MenuItem menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
+            => DoAddMenuItem(menu, caption, image, click, enabled, index);
+        public static MenuItem AddItem<T>(this ContextMenu menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true, int index = -1)
+            => DoAddMenuItem(menu, caption, image, tag, click, enabled, index);
+        public static MenuItem AddItem<T>(this MenuItem menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true, int index = -1)
+            => DoAddMenuItem(menu, caption, image, tag, click, enabled, index);
+        public static MenuItem AddCheckItem<T>(this ContextMenu menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked = false, bool enabled = true, int index = -1)
+            => DoAddCheckItem<T>(menu, caption, tag, click, isChecked, enabled, index);
+
+        #endregion
     }
 }