123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media.Imaging;
- using InABox.Core;
- using InABox.WPF;
- using InABox.Wpf;
- namespace InABox.DynamicGrid
- {
- /// <summary>
- /// Interaction logic for MasterList.xaml
- /// </summary>
- public partial class MasterList : ThemableWindow
- {
- private bool AllItems;
- private readonly bool AllowAllItems = true;
- private bool bLoaded;
- private readonly IDynamicGrid grid;
- /// <summary>
- ///
- /// </summary>
- /// <param name="type"></param>
- /// <param name="groupby"></param>
- /// <param name="selectedgroup"></param>
- /// <param name="allowallitems">Show an "All Items" entry at tge top of the Groups List Box</param>
- /// <param name="dataGrid">If null, finds a DynamicDataGrid of the specified entity type.
- /// Otherwise, MasterList uses this parameter as the DynamicGrid type</param>
- public MasterList(Type type, string? groupby = null, string? selectedgroup = null, bool allowallitems = false, Type? dataGrid = null)
- {
- InitializeComponent();
- Type = type;
- AllowAllItems = allowallitems;
- SelectedGroup = selectedgroup;
- CurrentGroup = selectedgroup;
- GroupBy = groupby;
- if(dataGrid == null)
- {
- dataGrid = DynamicGridUtils.FindDynamicGrid(typeof(DynamicDataGrid<>), type);
- }
- grid = (IDynamicGrid)Activator.CreateInstance(dataGrid);
- grid.Margin = new Thickness(5, 5, 5, 5);
- ((DependencyObject)grid).SetValue(Grid.ColumnProperty, 1);
- ((DependencyObject)grid).SetValue(Grid.RowProperty, 0);
- layoutGrid.Children.Add((UIElement)grid);
- grid.Options.BeginUpdate();
- grid.Options.AddRange(
- DynamicGridOption.SelectColumns,
- DynamicGridOption.FilterRows,
- DynamicGridOption.ShowHelp
- );
- DataModelType = typeof(AutoDataModel<>).MakeGenericType(type);
- if (DataModelType != null)
- grid.Options.Add(DynamicGridOption.Print);
- if (!string.IsNullOrWhiteSpace(GroupBy))
- grid.AddHiddenColumn(GroupBy);
- grid.Options.EndUpdate();
- grid.OnPrintData += PrintData;
- grid.OnAfterReload += Grid_AfterReload;
- grid.OnFilterRecord += Grid_OnFilterRecord;
- grid.OnCreateItem += Grid_OnCreateItem;
- grid.Refresh(true, true);
- }
- public Type Type { get; set; }
- private Type DataModelType { get; }
- public object? CurrentGroup { get; set; }
- public string? GroupBy { get; set; }
- public string? SelectedGroup { get; set; }
- public List<Tuple<string, object?, BitmapImage>> GroupList { get; private set; }
- private void Grid_AfterReload(object sender)
- {
- if (bLoaded)
- return;
- if (!string.IsNullOrEmpty(GroupBy))
- {
- CoreTable lookups = null;
- var col = grid.MasterColumns.First(x => x.ColumnName.Equals(GroupBy)).Editor as ILookupEditor;
- if (col != null)
- lookups = col.Values(GroupBy);
- //grid.ConfigureColumns(grid.MasterColumns, true);
- //grid.AddHiddenColumn(GroupBy);
- GroupList = new List<Tuple<string, object?, BitmapImage>>();
- if (AllowAllItems)
- GroupList.Add(new Tuple<string, object?, BitmapImage>("All Items", null, Properties.Resources.doc_misc.AsBitmapImage()));
- var column = grid.MasterColumns.Where(x => x.ColumnName.Equals(GroupBy)).FirstOrDefault();
- if (column != null)
- foreach (var row in grid.Data.Rows)
- {
- var entry = row[GroupBy];
- string display = "Unassigned";
- if (entry != null)
- {
- display = entry.ToString()!;
- if (lookups != null)
- {
- var lookup = lookups.Rows.FirstOrDefault(r => r[GroupBy] != null && r[GroupBy].Equals(entry));
- if (lookup != null)
- display = lookup.Get<string>("Display");
- }
- }
- if (!GroupList.Any(x => (x.Item1 == null && display == "") || (x.Item1 != null && x.Item1.Equals(display))))
- GroupList.Add(new Tuple<string, object?, BitmapImage>(entry != null ? display : "Unassigned", entry,
- Properties.Resources.doc_misc.AsBitmapImage()));
- }
- //foreach (var key in column.Lookups.Keys)
- // GroupList.Add(new Tuple<string, string, BitmapImage>(column.Lookups[key].ToString(), key.ToString(), Properties.Resources.edit.AsBitmapImage()));
- if (!string.IsNullOrWhiteSpace(SelectedGroup))
- if (!GroupList.Any(x => Equals(x.Item2, SelectedGroup)))
- GroupList.Add(new Tuple<string, object?, BitmapImage>(SelectedGroup, SelectedGroup,
- Properties.Resources.doc_misc.AsBitmapImage()));
- GroupList = GroupList.OrderBy(x => (x.Item1 == "All Items" ? "0" : x.Item1 == "Unassigned" ? "1" : "2") + x.Item1).ToList();
- Groups.ItemsSource = GroupList;
- if (string.IsNullOrEmpty(SelectedGroup))
- {
- CurrentGroup = "";
- Groups.Visibility = Visibility.Visible;
- }
- else
- {
- CurrentGroup = SelectedGroup;
- Groups.Visibility = Visibility.Collapsed;
- }
- }
- else
- {
- Groups.Visibility = Visibility.Collapsed;
- }
- //grid.Refresh(true, true);
- bLoaded = true;
- var item = GroupList != null
- ? GroupList.FirstOrDefault(x => (SelectedGroup == null && x.Item2 == null) || (x.Item2 != null && x.Item2.Equals(SelectedGroup)))
- : null;
- if (!AllowAllItems && item == null)
- item = GroupList?.FirstOrDefault();
- AllItems = AllowAllItems && item == null;
- Groups.SelectedIndex = item != null ? GroupList.IndexOf(item) : 0;
- Title = Type.Name + " Master List" + (item == null ? "" : " - " + item.Item1);
- Groups.Focus();
- }
- private void PrintData(object sender)
- {
- var filtertype = typeof(Filter<>).MakeGenericType(Type);
- var filter = Activator.CreateInstance(filtertype, "ID") as IFilter;
- filter.Expression = CoreUtils.CreateMemberExpression(Type, "ID"); //CoreUtils.GetPropertyExpression(Type, "ID");
- filter.Operator = Operator.InList;
- filter.Value = grid.SelectedRows.Select(r => r.Get<Guid>("ID")).ToArray();
- var model = Activator.CreateInstance(DataModelType, filter) as DataModel;
- //MethodInfo print = typeof(ReportUtils).GetMethod("PrintMenu").MakeGenericMethod(Type);
- //print.Invoke(null, new object[] { (FrameworkElement)sender, model, Security.CanEdit<Report>(), false });
- }
- private void Grid_OnCreateItem(object sender, object item)
- {
- if (!string.IsNullOrEmpty(GroupBy) && CurrentGroup != null)
- CoreUtils.SetPropertyValue(item, GroupBy, CurrentGroup);
- }
- private bool Grid_OnFilterRecord(CoreRow row)
- {
- if (AllItems)
- return true;
- var result = GroupBy == null || CurrentGroup == null || Equals(CurrentGroup, row[GroupBy]);
- return result; // ((CurrentGroup == null) && (row[GroupBy] == null)) || ((CurrentGroup != null) && (CurrentGroup.Equals(row[GroupBy])));
- }
- private void Groups_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (!bLoaded)
- return;
- var newCurrentGroup = CurrentGroup;
- if (e.AddedItems.Count == 0 || Groups.SelectedIndex == 0)
- {
- AllItems = AllowAllItems;
- newCurrentGroup = GroupList.Any() ? GroupList.First().Item2 : null;
- }
- else
- {
- AllItems = false;
- var selected = (Tuple<string, object, BitmapImage>)e.AddedItems[0];
- newCurrentGroup = selected.Item2;
- }
- if (!Equals(newCurrentGroup, CurrentGroup))
- {
- CurrentGroup = newCurrentGroup;
- grid.Refresh(false, false);
- }
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- var center = new Point(Left + Width / 2, Top + Height / 2);
- var screen = WpfScreen.GetScreenFrom(this);
- var maxwidth = (int)screen.WorkingArea.Width - 200;
- var DesiredWidth = Math.Max(600, grid.DesiredWidth());
- if (DesiredWidth > maxwidth)
- DesiredWidth = maxwidth;
- if (DesiredWidth > Width)
- Width = DesiredWidth;
- if (!string.IsNullOrEmpty(GroupBy))
- Width += Groups.DesiredSize.Width + 45.0F;
- var maxheight = (int)screen.WorkingArea.Height - 200;
- var DesiredHeight = (int)(Width * 0.75);
- if (DesiredHeight > maxheight)
- DesiredHeight = maxheight;
- if (DesiredHeight > Height)
- Height = DesiredHeight;
- //WpfScreen.CenterWindowOnScreen(this);
- Left = center.X - Width / 2;
- Top = center.Y - Height / 2;
- //Left = screen.DeviceBounds.Left + ((screen.DeviceBounds.Width - Width) / 2.0F);
- //Top = screen.DeviceBounds.Top + ((screen.DeviceBounds.Height - Height) / 2.0F);
- }
- }
- }
|