فهرست منبع

Fix to CoreUtils.ChangeType when converting arrays. Extracted context menu logic of DynamicTreeView into an event, allowing for custom logic.
Added hyperlink fix to RichTextEditor

Kenric Nugteren 2 سال پیش
والد
کامیت
43f397c34f

+ 7 - 0
InABox.Core/CoreUtils.cs

@@ -490,8 +490,12 @@ namespace InABox.Core
 
             if (type.GetTypeInfo().IsEnum)
             {
+                if (value.GetType().IsArray)
+                    return (value as Array).Cast<object>().Select(x => Enum.ToObject(type, x)).ToArray();
                 if (value is string)
                     return Enum.Parse(type, value as string);
+                if (value is IEnumerable<object> list)
+                    return list.Select(x => Enum.ToObject(type, x)).ToArray();
                 return Enum.ToObject(type, value);
             }
 
@@ -512,6 +516,9 @@ namespace InABox.Core
                     return Guid.Empty;
                 }
 
+            if (value is IEnumerable<object> objList && type == typeof(Guid))
+                return objList.Select(x => (Guid)ChangeType(x, typeof(Guid))).ToArray();
+
             if (value is byte[] && type == typeof(Guid))
                 return new Guid(value as byte[]);
 

+ 1 - 1
InABox.DynamicGrid/Columns/DynamicActionColumn.cs

@@ -24,7 +24,7 @@ namespace InABox.DynamicGrid
         public Func<CoreRow?, BitmapImage?> Image { get; protected set; }
         public Func<CoreRow?, bool>? Action { get; protected set; }
 
-        public Func<DynamicActionColumn, CoreRow, FrameworkElement>? ToolTip { get; set; }
+        public Func<DynamicActionColumn, CoreRow?, FrameworkElement?>? ToolTip { get; set; }
         public DynamicActionColumnPosition Position { get; set; }
 
         public string[] SelectedFilters { get; set; }

+ 62 - 11
InABox.DynamicGrid/DynamicTreeView.cs

@@ -133,6 +133,8 @@ namespace InABox.DynamicGrid
     }
 
     public delegate void OnSelectItem(DynamicTreeNode node);
+
+    public delegate void OnContextMenuOpening(DynamicTreeNode node, ContextMenu menu);
     
     public abstract class DynamicTreeView<T> : ContentControl where T : BaseObject, new()
     {
@@ -143,17 +145,18 @@ namespace InABox.DynamicGrid
 
         protected CoreTable Data { get; private set; }
 
-        private ContextMenu _menu = null;
-        private SfTreeGrid _tree = null;
-        private DockPanel _dock = null;
-        private Grid _grid = null;
-        private Button _add = null;
-        private Button _edit = null;
-        private Button _delete = null;
-        private Label _spacer = null;
+        private ContextMenu _menu;
+        private SfTreeGrid _tree;
+        private DockPanel _dock;
+        private Grid _grid;
+        private Button _add;
+        private Button _edit;
+        private Button _delete;
+        private Label _spacer;
 
         public FluentList<DynamicTreeOption> Options { get; private set; }
         public event OnSelectItem OnSelectItem;
+        public event OnContextMenuOpening OnContextMenuOpening;
 
         private double minRowHeight = 30D;
         private double maxRowHeight = 30D;
@@ -208,7 +211,8 @@ namespace InABox.DynamicGrid
             _tree.HeaderRowHeight = 0D;
             _tree.SelectionChanged += (o,e) => OnSelectItem?.Invoke(_tree.SelectedItem as DynamicTreeNode);
             _tree.AllowSelectionOnExpanderClick = false;
-            
+
+            _tree.ContextMenuOpening += _tree_ContextMenuOpening;
             _tree.ContextMenu = _menu;
             _tree.Background = new SolidColorBrush(Colors.DimGray);
             
@@ -258,6 +262,54 @@ namespace InABox.DynamicGrid
             SizeChanged += DynamicTreeView_SizeChanged;
         }
 
+        #region Public Interface
+
+        public void AddItem(DynamicTreeNode? parentNode = null, bool edit = true)
+        {
+            var id = parentNode?.ID ?? Guid.Empty;
+
+            try
+            {
+                T item = DoCreateItem(id);
+                if (edit)
+                {
+                    if (DoEditItem(item))
+                    {
+                        DoSaveItem(item);
+                        Refresh();
+                    }
+                }
+                else
+                {
+                    DoSaveItem(item);
+                    Refresh();
+                }
+            }
+            catch (Exception e)
+            {
+                MessageBox.Show(e.Message);
+            }
+        }
+
+        #endregion
+
+        private void _tree_ContextMenuOpening(object sender, ContextMenuEventArgs e)
+        {
+            _menu.Items.Clear();
+            if (OnContextMenuOpening is not null)
+            {
+                OnContextMenuOpening.Invoke((_tree.SelectedItem as DynamicTreeNode)!, _menu);
+                if(_menu.Items.Count == 0)
+                {
+                    e.Handled = true;
+                }
+            }
+            else
+            {
+                _menu.AddItem("Add Item", null, (_tree.SelectedItem as DynamicTreeNode)!.ID, AddItem);
+            }
+        }
+
         private void DynamicTreeView_SizeChanged(object sender, SizeChangedEventArgs e)
         {
             CalculateRowHeight();
@@ -324,7 +376,6 @@ namespace InABox.DynamicGrid
         protected virtual T DoCreateItem(Guid parent)
         {
             T result = new T();
-            var node = _tree.SelectedItem as DynamicTreeNode;
             CoreUtils.SetPropertyValue(result, CoreUtils.GetFullPropertyName(ParentID, "."), parent);
             return result;
         }
@@ -356,7 +407,7 @@ namespace InABox.DynamicGrid
             catch (Exception e)
             {
                 MessageBox.Show(e.Message);
-            }           
+            }
         }
         
         private void EditItem(Button button)

+ 2 - 1
InABox.DynamicGrid/Editors/RichTextEditor.xaml

@@ -69,7 +69,8 @@
             <sf:SfRichTextBoxAdv x:Name="Editor" DockPanel.Dock="Top" EnableMiniToolBar="False"
                                  ContentChanged="RichTextBoxAdv_ContentChanged" BorderThickness="0"
                                  LostFocus="RichTextBoxAdv_LostFocus" OverridesDocumentBackground="True"
-                                 LayoutType="Continuous" />
+                                 LayoutType="Continuous"
+                                 RequestNavigate="Editor_RequestNavigate"/>
 
         </DockPanel>
     </Border>

+ 9 - 1
InABox.DynamicGrid/Editors/RichTextEditor.xaml.cs

@@ -1,4 +1,5 @@
-using System.IO;
+using System.Diagnostics;
+using System.IO;
 using System.Text;
 using System.Windows;
 using System.Windows.Controls;
@@ -163,5 +164,12 @@ namespace InABox.DynamicGrid
         {
             Editor.ZoomFactor = Editor.ZoomFactor / 1.5F;
         }
+
+        private void Editor_RequestNavigate(object obj, RequestNavigateEventArgs args)
+        {
+            var processStartInfo = new ProcessStartInfo(args.Hyperlink.NavigationLink);
+            processStartInfo.UseShellExecute = true;
+            Process.Start(processStartInfo);
+        }
     }
 }