Jelajahi Sumber

Added function to set title of multiselectdialog

Kenric Nugteren 1 tahun lalu
induk
melakukan
7c9d53a2d8
1 mengubah file dengan 35 tambahan dan 13 penghapusan
  1. 35 13
      inabox.wpf/DynamicGrid/MultiSelectDialog.cs

+ 35 - 13
inabox.wpf/DynamicGrid/MultiSelectDialog.cs

@@ -2,6 +2,7 @@
 using System.ComponentModel;
 using System.ComponentModel;
 using System.Linq;
 using System.Linq;
 using System.Linq.Expressions;
 using System.Linq.Expressions;
+using System.Runtime.CompilerServices;
 using System.Windows;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Controls;
 using System.Windows.Forms;
 using System.Windows.Forms;
@@ -112,27 +113,41 @@ namespace InABox.DynamicGrid
             CancelButton.SetValue(Grid.ColumnProperty, 3);
             CancelButton.SetValue(Grid.ColumnProperty, 3);
             grid.Children.Add(CancelButton);
             grid.Children.Add(CancelButton);
         }
         }
+        public bool ShowDialog(string? column = null, string? value = null, FilterType filtertype = FilterType.Contains) =>
+            ShowDialogInternal(null, column, value, filtertype);
+        public bool ShowDialog(string title) =>
+            ShowDialogInternal(title, null, null, default);
 
 
-        public bool ShowDialog(String? column = null, String? value = null, FilterType filtertype = FilterType.Contains)
+        private bool ShowDialogInternal(string? title, string? column, string? value, FilterType filtertype)
         {
         {
-            window = new ThemableWindow { Title = "Select Items", WindowStyle = WindowStyle.SingleBorderWindow };
-            window.Content = grid;
+            window = new ThemableWindow
+            {
+                Title = title ?? "Select Items",
+                WindowStyle = WindowStyle.SingleBorderWindow,
+                Content = grid
+            };
             datagrid.Refresh(true, true);
             datagrid.Refresh(true, true);
-            if (!String.IsNullOrEmpty(column) && !String.IsNullOrEmpty(value))
+            if (!column.IsNullOrWhiteSpace() && !value.IsNullOrWhiteSpace())
                 datagrid.AddVisualFilter(column, value, filtertype);
                 datagrid.AddVisualFilter(column, value, filtertype);
             if (window.ShowDialog() == true)
             if (window.ShowDialog() == true)
                 return true;
                 return true;
             return false;
             return false;
         }
         }
 
 
+        private Window GetWindow([CallerMemberName] string methodName = "")
+        {
+            return window ?? throw new Exception($"Must call ShowDialog() before {methodName}()");
+        }
+
         public Guid[] IDs()
         public Guid[] IDs()
         {
         {
-            if (window?.DialogResult == true)
+            var window = GetWindow();
+            if (window.DialogResult == true)
             {
             {
                 if (datagrid?.Data != null && datagrid.SelectedRows.Any())
                 if (datagrid?.Data != null && datagrid.SelectedRows.Any())
                     return datagrid.SelectedRows.Select(r => r.Get<T, Guid>(x => x.ID)).ToArray();
                     return datagrid.SelectedRows.Select(r => r.Get<T, Guid>(x => x.ID)).ToArray();
             }
             }
-            else if (window?.DialogResult == false)
+            else if (window.DialogResult == false)
                 return new Guid[] { Guid.Empty };
                 return new Guid[] { Guid.Empty };
 
 
             return Array.Empty<Guid>();
             return Array.Empty<Guid>();
@@ -140,12 +155,17 @@ namespace InABox.DynamicGrid
 
 
         public CoreTable Data()
         public CoreTable Data()
         {
         {
+            var window = GetWindow();
+
             var result = new CoreTable();
             var result = new CoreTable();
             result.Columns.Add(new CoreColumn { ColumnName = "ID", DataType = typeof(Guid) });
             result.Columns.Add(new CoreColumn { ColumnName = "ID", DataType = typeof(Guid) });
-            foreach (var column in _columns.Items)
-                result.Columns.Add(new CoreColumn { ColumnName = column.Property, DataType = column.Type });
+            if(_columns is not null)
+            {
+                foreach (var column in _columns.Items)
+                    result.Columns.Add(new CoreColumn { ColumnName = column.Property, DataType = column.Type });
+            }
 
 
-            if (window?.DialogResult == true)
+            if (window.DialogResult == true)
             {
             {
 
 
                 if (datagrid?.Data != null && datagrid.SelectedRows.Any())
                 if (datagrid?.Data != null && datagrid.SelectedRows.Any())
@@ -160,7 +180,7 @@ namespace InABox.DynamicGrid
                         result.Rows.Add(row);
                         result.Rows.Add(row);
                     }
                     }
             }
             }
-            else if (window?.DialogResult == false)
+            else if (window.DialogResult == false)
             {
             {
                 var row = result.NewRow(true);
                 var row = result.NewRow(true);
                 result.Rows.Add(row);
                 result.Rows.Add(row);
@@ -171,7 +191,9 @@ namespace InABox.DynamicGrid
         
         
         public T[] Items(Columns<T>? columns = null)
         public T[] Items(Columns<T>? columns = null)
         {
         {
-            if (window?.DialogResult == true)
+            var window = GetWindow();
+
+            if (window.DialogResult == true)
             {
             {
                 if (datagrid.Data != null && datagrid.SelectedRows.Any())
                 if (datagrid.Data != null && datagrid.SelectedRows.Any())
                 {
                 {
@@ -186,7 +208,7 @@ namespace InABox.DynamicGrid
                     return items.ToArray();
                     return items.ToArray();
                 }
                 }
             }
             }
-            else if (window?.DialogResult == false)
+            else if (window.DialogResult == false)
                 return new T[] { new T() };
                 return new T[] { new T() };
 
 
             return Array.Empty<T>();
             return Array.Empty<T>();
@@ -206,7 +228,7 @@ namespace InABox.DynamicGrid
 
 
         private void CancelButton_Click(object sender, RoutedEventArgs e)
         private void CancelButton_Click(object sender, RoutedEventArgs e)
         {
         {
-            window.Close();
+            window!.Close();
         }
         }
 
 
         private void OKButton_Click(object sender, RoutedEventArgs e)
         private void OKButton_Click(object sender, RoutedEventArgs e)