123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Windows;
- using InABox.Core;
- using InABox.Wpf;
- using Syncfusion.UI.Xaml.Grid;
- namespace InABox.DynamicGrid
- {
- /// <summary>
- /// Interaction logic for DynamicMultiEditWindow.xaml
- /// </summary>
- public partial class DynamicMultiEditWindow : ThemableWindow, IDynamicEditorForm
- {
- public DynamicMultiEditWindow()
- {
- InitializeComponent();
- }
- protected virtual void EndEdit(int row, int col, object value)
- {
- }
- protected virtual void DeleteRow(int row)
- {
- }
- private void Editor_CurrentCellEndEdit(object sender, CurrentCellEndEditEventArgs e)
- {
- //if (!(Editor.CurrentCellInfo.Column is GridComboBoxColumn))
- // EndEdit(e.RowColumnIndex.RowIndex-1, e.RowColumnIndex.ColumnIndex, null);
- }
- private void OK_Click(object sender, RoutedEventArgs e)
- {
- DialogResult = true;
- Close();
- }
- private void Cancel_Click(object sender, RoutedEventArgs e)
- {
- DialogResult = false;
- Close();
- }
- private void CurrentCellActivated(object sender, CurrentCellActivatedEventArgs args)
- {
- //Skip the selection when it is moved to next row on adding new row.
- //bool needToMove = (Editor.IsAddNewIndex(args.PreviousRowColumnIndex.RowIndex)
- // //&& !Editor.IsAddNewIndex(args.CurrentRowColumnIndex.RowIndex)
- // && (args.ActivationTrigger == ActivationTrigger.Keyboard)
- // && (Keyboard.IsKeyDown(Key.Enter) || Keyboard.IsKeyDown(Key.Tab)));
- //if (needToMove)
- //{
- // Dispatcher.BeginInvoke(
- // new Action(
- // () =>
- // {
- // Editor.MoveCurrentCell
- // (
- // new Syncfusion.UI.Xaml.ScrollAxis.RowColumnIndex
- // (
- // args.PreviousRowColumnIndex.RowIndex-1,
- // args.CurrentRowColumnIndex.ColumnIndex
- // )
- // );
- // }
- // )
- // );
- //}
- }
- private void Delete_Click(object sender, RoutedEventArgs e)
- {
- DeleteRow(Editor.CurrentCellInfo.RowIndex);
- }
- private void Editor_CurrentCellValueChanged(object sender, CurrentCellValueChangedEventArgs e)
- {
- //EndEdit(e.RowColumnIndex.RowIndex - 1, e.RowColumnIndex.ColumnIndex);
- }
- private void Editor_CurrentCellDropDownSelectionChanged(object sender, CurrentCellDropDownSelectionChangedEventArgs e)
- {
- //EndEdit(e.RowColumnIndex.RowIndex - 1, e.RowColumnIndex.ColumnIndex, (e.SelectedItem as DataRowView)["ID"]);
- }
- }
- public class DynamicEditWindow<T> : DynamicMultiEditWindow where T : BaseObject, new()
- {
- private CoreTable _data;
- private readonly DataTable table = new();
- private readonly Dictionary<int, T> updates = new();
- public T[] Updates => updates.Values.ToArray();
- public event OnCustomiseColumns? OnCustomiseColumns;
- public event OnGetEditor? OnGetEditor;
- public event OnCreateItem<T>? OnCreateItem;
- public event OnGetEditorSequence? OnGetSequence;
- private decimal GetSequence(DynamicGridColumn column)
- {
- if (OnGetSequence != null)
- return OnGetSequence.Invoke(column);
- return 999M;
- }
- public void Load(CoreTable data)
- {
- _data = data;
- var dgcs = OnCustomiseColumns?.Invoke(this, null).OrderBy(x => GetSequence(x)).ToArray();
- foreach (var dgc in dgcs)
- {
- var col = data.Columns.FirstOrDefault(x => string.Equals(x.ColumnName, dgc.ColumnName));
- if (col != null)
- table.Columns.Add(col.ColumnName, col.DataType);
- }
- foreach (var row in data.Rows)
- {
- var newrow = table.NewRow();
- foreach (var col in dgcs)
- if (table.Columns.Contains(col.ColumnName))
- newrow[col.ColumnName] = row[col.ColumnName];
- table.Rows.Add(newrow);
- }
- foreach (var dgc in dgcs)
- try
- {
- var editor = OnGetEditor?.Invoke(dgc);
- if (editor != null && editor.Editable != Editable.Hidden)
- {
- GridColumn col = null;
- if (editor is TextBoxEditor)
- {
- col = new GridTextColumn { ColumnSizer = GridLengthUnitType.Star };
- }
- else if (editor is CodeEditor)
- {
- col = new GridMaskColumn { Mask = ">a" };
- }
- else if (editor is CheckBoxEditor)
- {
- col = new GridCheckBoxColumn();
- }
- else if (editor is DateTimeEditor)
- {
- col = new GridDateTimeColumn();
- }
- else if (editor is DateEditor)
- {
- col = new GridDateTimeColumn();
- }
- else if (editor is TimeOfDayEditor)
- {
- col = new GridTimeSpanColumn();
- }
- else if (editor is DurationEditor)
- {
- col = new GridTimeSpanColumn();
- }
- else if (editor is PINEditor)
- {
- col = new GridMaskColumn { Mask = "9999" };
- }
- // //else if (editor is CheckListEditor)
- // // element = new CheckListBoxEditorControl();
- // //else if (editor is MemoEditor)
- // // element = new MemoEditorControl();
- else if (editor is LookupEditor || editor is EnumLookupEditor || editor is ComboLookupEditor)
- {
- var values = (editor as ILookupEditor)?.Values(dgc.ColumnName)?.ToDataTable()?.AsDataView();
- col = new GridComboBoxColumn { DisplayMemberPath = "Display", SelectedValuePath = "ID", ItemsSource = values };
- }
- // //else if (editor is MiscellaneousDocumentEditor)
- // // element = new DocumentEditorControl();
- // //else if (editor is ImageDocumentEditor)
- // // element = new DocumentEditorControl();
- // //else if (editor is PDFDocumentEditor)
- // // element = new DocumentEditorControl();
- else if (editor is CurrencyEditor)
- {
- col = new GridCurrencyColumn();
- }
- else if (editor is DoubleEditor)
- {
- col = new GridNumericColumn { ParsingMode = ParseMode.Double };
- }
- else if (editor is IntegerEditor)
- {
- col = new GridNumericColumn { ParsingMode = ParseMode.Int };
- }
- //else if (editor is InABox.Core.ScriptEditor)
- // element = new ScriptEditorControl();
- //else if (editor is PopupEditor)
- // element = new PopupEditorControl();
- //else if (editor is TimestampEditor)
- // element = new TimestampEditorControl();
- //else if (editor is ColorEditor)
- // element = new ColorEditorControl();
- if (col != null)
- {
- col.MappingName = dgc.ColumnName;
- col.UseBindingValue = true;
- Editor.Columns.Add(col);
- }
- }
- }
- catch (Exception e)
- {
- Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
- }
- }
- protected override void EndEdit(int row, int col, object value)
- {
- if (!updates.ContainsKey(row))
- {
- if (row < _data.Rows.Count)
- updates[row] = _data.Rows[row].ToObject<T>();
- else
- updates[row] = OnCreateItem?.Invoke();
- }
- object cur = updates[row];
- var property = Editor.Columns[col].MappingName;
- var currow = Editor.View.IsAddingNew
- ? (Editor.View.CurrentAddItem as DataRowView).Row
- : table.Rows[row];
- var val = value == null ? currow[property] : value;
- currow[property] = val;
- CoreUtils.SetPropertyValue(cur, property, val);
- }
- protected override void OnActivated(EventArgs e)
- {
- base.OnActivated(e);
- Editor.ItemsSource = table;
- }
- protected override void DeleteRow(int row)
- {
- if (Editor.IsAddNewIndex(row))
- Editor.View.CancelNew();
- else if (row < table.Rows.Count)
- table.Rows.RemoveAt(row);
- }
- }
- }
|