diff --git a/Elwig/App.xaml.cs b/Elwig/App.xaml.cs index bef7d46..1e5ffbc 100644 --- a/Elwig/App.xaml.cs +++ b/Elwig/App.xaml.cs @@ -248,7 +248,7 @@ namespace Elwig { public static BaseDataWindow FocusBaseDataSeason(int year) { var w = FocusBaseData(); w.Seasons.Focus(); - ControlUtils.SelectListBoxItem(w.SeasonList, s => (s as Season)?.Year, year); + ControlUtils.SelectItemWithPk(w.SeasonList, year); return w; } diff --git a/Elwig/Helpers/Billing/Varibute.cs b/Elwig/Helpers/Billing/Varibute.cs index d89c662..cdc1b42 100644 --- a/Elwig/Helpers/Billing/Varibute.cs +++ b/Elwig/Helpers/Billing/Varibute.cs @@ -1,4 +1,5 @@ using Elwig.Models.Entities; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; @@ -45,6 +46,7 @@ namespace Elwig.Helpers.Billing { } } + [PrimaryKey("Listing")] public class Varibute : IComparable { public WineVar? Variety { get; } diff --git a/Elwig/Helpers/ControlUtils.cs b/Elwig/Helpers/ControlUtils.cs index d0b075a..fcc014b 100644 --- a/Elwig/Helpers/ControlUtils.cs +++ b/Elwig/Helpers/ControlUtils.cs @@ -84,13 +84,13 @@ namespace Elwig.Helpers { return null; } - public static void RenewItemsSource(Selector selector, IEnumerable? source, Func getId, SelectionChangedEventHandler? handler = null, RenewSourceDefault def = RenewSourceDefault.None) { + public static void RenewItemsSource(Selector selector, IEnumerable? source, SelectionChangedEventHandler? handler = null, RenewSourceDefault def = RenewSourceDefault.None) { if (selector.ItemsSource == source) return; - var selectedId = getId(selector.SelectedItem); + var selectedId = Utils.GetEntityIdentifier(selector.SelectedItem); object? selItem = null; - if (selectedId != null && source != null) - selItem = source.Cast().FirstOrDefault(i => selectedId.Equals(getId(i))); + if (selectedId != 0 && source != null) + selItem = source.Cast().FirstOrDefault(i => selectedId.Equals(Utils.GetEntityIdentifier(i))); if (source != null && selItem == null) { if ((def == RenewSourceDefault.IfOnly && source.Cast().Count() == 1) || def == RenewSourceDefault.First) { selItem = source.Cast().First(); @@ -102,30 +102,30 @@ namespace Elwig.Helpers { selector.SelectedItem = selItem; } - public static void RenewItemsSource(Xceed.Wpf.Toolkit.Primitives.Selector selector, IEnumerable? source, Func getId, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventHandler? handler = null) { + public static void RenewItemsSource(Xceed.Wpf.Toolkit.Primitives.Selector selector, IEnumerable? source, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventHandler? handler = null) { if (selector.ItemsSource == source) return; - var selectedIds = selector.SelectedItems.Cast().Select(i => getId(i)).ToList(); + var selectedIds = selector.SelectedItems.Cast().Select(i => Utils.GetEntityIdentifier(i)).ToList(); if (handler != null && selectedIds != null) selector.ItemSelectionChanged -= handler; selector.ItemsSource = source; if (source != null) { selector.SelectedItems.Clear(); - foreach (var i in source.Cast().Where(i => selectedIds.Contains(getId(i)))) + foreach (var i in source.Cast().Where(i => selectedIds.Contains(Utils.GetEntityIdentifier(i)))) selector.SelectedItems.Add(i); } if (handler != null && selectedIds != null) selector.ItemSelectionChanged += handler; } - public static void RenewItemsSource(DataGrid dataGrid, IEnumerable? source, Func getId, SelectionChangedEventHandler? handler = null, RenewSourceDefault def = RenewSourceDefault.None, bool keepSort = true) { + public static void RenewItemsSource(DataGrid dataGrid, IEnumerable? source, SelectionChangedEventHandler? handler = null, RenewSourceDefault def = RenewSourceDefault.None, bool keepSort = true) { if (dataGrid.ItemsSource == source) return; var column = dataGrid.CurrentCell.Column; var sortColumns = dataGrid.Columns.Select(c => c.SortDirection).ToList(); var sort = dataGrid.Items.SortDescriptions.ToList(); - var selectedId = getId(dataGrid.SelectedItem); + var selectedId = Utils.GetEntityIdentifier(dataGrid.SelectedItem); object? selItem = null; - if (selectedId != null && source != null) - selItem = source.Cast().FirstOrDefault(i => selectedId.Equals(getId(i))); + if (selectedId != 0 && source != null) + selItem = source.Cast().FirstOrDefault(i => selectedId.Equals(Utils.GetEntityIdentifier(i))); if (source != null && selItem == null) { if ((def == RenewSourceDefault.IfOnly && source.Cast().Count() == 1) || def == RenewSourceDefault.First) { selItem = source.Cast().First(); @@ -145,13 +145,13 @@ namespace Elwig.Helpers { dataGrid.CurrentCell = new(dataGrid.SelectedItem, column); } - public static void RenewItemsSource(ListBox listBox, IEnumerable? source, Func getId, SelectionChangedEventHandler? handler = null, RenewSourceDefault def = RenewSourceDefault.None) { + public static void RenewItemsSource(ListBox listBox, IEnumerable? source, SelectionChangedEventHandler? handler = null, RenewSourceDefault def = RenewSourceDefault.None) { if (listBox.ItemsSource == source) return; - var selectedId = getId(listBox.SelectedItem); + var selectedId = Utils.GetEntityIdentifier(listBox.SelectedItem); object? selItem = null; - if (selectedId != null && source != null) - selItem = source.Cast().FirstOrDefault(i => selectedId.Equals(getId(i))); + if (selectedId != 0 && source != null) + selItem = source.Cast().FirstOrDefault(i => selectedId.Equals(Utils.GetEntityIdentifier(i))); if (source != null && selItem == null) { if ((def == RenewSourceDefault.IfOnly && source.Cast().Count() == 1) || def == RenewSourceDefault.First) { selItem = source.Cast().FirstOrDefault(); @@ -163,61 +163,65 @@ namespace Elwig.Helpers { listBox.SelectedItem = selItem; } - public static object? GetItemFromSource(IEnumerable source, Func getId, object? id) { + public static object? GetItemFromSource(IEnumerable source, int? hash) { if (source == null) return null; var items = source.Cast(); - var item = items.Where(i => getId(i)?.Equals(id) ?? false).FirstOrDefault(); + var item = items.Where(i => Utils.GetEntityIdentifier(i) == hash).FirstOrDefault(); if (item == null && items.Any(i => i is NullItem)) return items.Where(i => i is NullItem).First(); return item; } - public static object? GetItemFromSource(IEnumerable source, object? item, Func getId) { - return GetItemFromSource(source, getId, getId(item)); + public static object? GetItemFromSource(IEnumerable source, object? item) { + return GetItemFromSource(source, Utils.GetEntityIdentifier(item)); } - public static void SelectComboBoxItem(ComboBox cb, Func getId, object? id) { - cb.SelectedItem = GetItemFromSource(cb.ItemsSource, getId, id); + public static void SelectItemWithHash(Selector input, int? hash) { + if (hash == null) { + input.SelectedItem = null; + } else { + input.SelectedItem = GetItemFromSource(input.ItemsSource, (int)hash); + } + if (input is ListBox lb) { + lb.ScrollIntoView(lb.SelectedItem); + } else if (input is DataGrid dg) { + dg.ScrollIntoView(dg.SelectedItem); + } } - public static void SelectComboBoxItem(ComboBox cb, object? item, Func getId) { - SelectComboBoxItem(cb, getId, getId(item)); + public static void SelectItemWithPk(Selector input, params object?[] pk) { + SelectItemWithHash(input, Utils.GetEntityIdentifier(pk)); } - public static void SelectListBoxItem(ListBox lb, Func getId, object? id) { - lb.SelectedItem = GetItemFromSource(lb.ItemsSource, getId, id); - lb.ScrollIntoView(lb.SelectedItem); + public static void SelectItem(Selector input, object? item) { + SelectItemWithHash(input, Utils.GetEntityIdentifier(item)); } - public static void SelectListBoxItem(ListBox lb, object? item, Func getId) { - SelectListBoxItem(lb, getId, getId(item)); - } - - public static IEnumerable GetItemsFromSource(IEnumerable source, Func getId, IEnumerable ids) { + public static IEnumerable GetItemsFromSource(IEnumerable source, IEnumerable ids) { if (source == null) - return Array.Empty(); - return source.Cast().Where(i => ids.Any(c => c?.Equals(getId(i)) ?? false)); + return []; + return source.Cast().Where(i => ids.Any(c => c == Utils.GetEntityIdentifier(i))); } - public static IEnumerable GetItemsFromSource(IEnumerable source, IEnumerable? items, Func getId) { + public static IEnumerable GetItemsFromSource(IEnumerable source, IEnumerable? items) { if (items == null) - return Array.Empty(); - return GetItemsFromSource(source, getId, items.Select(i => getId(i))); + return []; + return GetItemsFromSource(source, items.Select(Utils.GetEntityIdentifier)); } - public static void SelectCheckComboBoxItems(Xceed.Wpf.Toolkit.CheckComboBox ccb, Func getId, IEnumerable? ids) { + public static void SelectItems(Xceed.Wpf.Toolkit.CheckComboBox ccb, IEnumerable? ids) { ccb.SelectedItems.Clear(); if (ids == null) return; foreach (var id in ids) - ccb.SelectedItems.Add(GetItemFromSource(ccb.ItemsSource, getId, id)); + ccb.SelectedItems.Add(GetItemFromSource(ccb.ItemsSource, id)); } - public static void SelectCheckComboBoxItems(Xceed.Wpf.Toolkit.CheckComboBox ccb, IEnumerable? items, Func getId) { - SelectCheckComboBoxItems(ccb, getId, items?.Select(i => getId(i))); + public static void SelectItems(Xceed.Wpf.Toolkit.CheckComboBox ccb, IEnumerable? items) { + SelectItems(ccb, items?.Select(Utils.GetEntityIdentifier)); } - public static int GetInputHashCode(Control input) { + public static int? GetInputHashCode(Control input) { if (input is TextBox tb) { return Utils.GetEntityIdentifier(tb.Text); } else if (input is ComboBox sb) { @@ -229,7 +233,7 @@ namespace Elwig.Helpers { } else if (input is RadioButton rb) { return Utils.GetEntityIdentifier(rb.IsChecked); } else { - return 0; + return null; } } } diff --git a/Elwig/Helpers/Utils.cs b/Elwig/Helpers/Utils.cs index 10aa6c0..ad892f0 100644 --- a/Elwig/Helpers/Utils.cs +++ b/Elwig/Helpers/Utils.cs @@ -435,17 +435,17 @@ namespace Elwig.Helpers { return client; } - public static int GetEntityIdentifier(object? obj) { + public static int? GetEntityIdentifier(object? obj) { if (obj == null) { - return 0; - } else if (obj is IEnumerable list) { - var arr = list.Cast().Select(o => GetEntityIdentifier(o)).ToArray(); - return ((IStructuralEquatable)arr).GetHashCode(EqualityComparer.Default); - } else if (obj.GetType().GetCustomAttribute(typeof(PrimaryKeyAttribute), false) is not PrimaryKeyAttribute pkAttr) { - return obj.GetHashCode(); - } else { + return null; + } else if (obj is IEnumerable list && obj is not string) { + var arr = list.Cast().Select(GetEntityIdentifier).ToArray(); + return ((IStructuralEquatable)arr).GetHashCode(EqualityComparer.Default); + } else if (obj.GetType().GetCustomAttribute(typeof(PrimaryKeyAttribute), true) is PrimaryKeyAttribute pkAttr) { var pk = pkAttr.PropertyNames.Select(name => obj.GetType().GetProperty(name)!.GetValue(obj)?.GetHashCode() ?? 0).ToArray(); return ((IStructuralEquatable)pk).GetHashCode(EqualityComparer.Default); + } else { + return obj.GetHashCode(); } } } diff --git a/Elwig/Windows/AdministrationWindow.cs b/Elwig/Windows/AdministrationWindow.cs index b8525f2..0478174 100644 --- a/Elwig/Windows/AdministrationWindow.cs +++ b/Elwig/Windows/AdministrationWindow.cs @@ -198,15 +198,15 @@ namespace Elwig.Windows { protected void FillOriginalValues() { foreach (var tb in TextBoxInputs) - OriginalValues[tb] = Utils.GetEntityIdentifier(tb.Text); + OriginalValues[tb] = ControlUtils.GetInputHashCode(tb); foreach (var cb in ComboBoxInputs) - OriginalValues[cb] = Utils.GetEntityIdentifier(cb.SelectedItem); + OriginalValues[cb] = ControlUtils.GetInputHashCode(cb); foreach (var ccb in CheckComboBoxInputs) - OriginalValues[ccb] = Utils.GetEntityIdentifier(ccb.SelectedItems); + OriginalValues[ccb] = ControlUtils.GetInputHashCode(ccb); foreach (var cb in CheckBoxInputs) - OriginalValues[cb] = Utils.GetEntityIdentifier(cb.IsChecked); + OriginalValues[cb] = ControlUtils.GetInputHashCode(cb); foreach (var rb in RadioButtonInputs) - OriginalValues[rb] = Utils.GetEntityIdentifier(rb.IsChecked); + OriginalValues[rb] = ControlUtils.GetInputHashCode(rb); } protected void SetOriginalValue(Control input, object? value) { @@ -232,7 +232,7 @@ namespace Elwig.Windows { protected void SetDefaultValue(Control input, object? value) { if (input is UnitTextBox utbx) input = utbx.TextBox; - DefaultValues[input] = value != null ? Utils.GetEntityIdentifier(value) : null; + DefaultValues[input] = Utils.GetEntityIdentifier(value); if (!InputHasChanged(input)) { if (InputIsNotDefault(input)) { ControlUtils.SetInputNotDefault(input); @@ -278,39 +278,21 @@ namespace Elwig.Windows { protected bool InputHasChanged(Control input) { if (input is UnitTextBox utbx) input = utbx.TextBox; - if (!OriginalValues.ContainsKey(input)) { + if (!OriginalValues.TryGetValue(input, out int? original)) { return false; - } else if (input is TextBox tb) { - return OriginalValues[tb] != Utils.GetEntityIdentifier(tb.Text); - } else if (input is ComboBox sb) { - return OriginalValues[sb] != Utils.GetEntityIdentifier(sb.SelectedItem); - } else if (input is CheckComboBox ccb) { - return OriginalValues[ccb] != Utils.GetEntityIdentifier(ccb.SelectedItems); - } else if (input is CheckBox cb) { - return OriginalValues[cb] != Utils.GetEntityIdentifier(cb.IsChecked); - } else if (input is RadioButton rb) { - return OriginalValues[rb] != Utils.GetEntityIdentifier(rb.IsChecked); } else { - return false; + var current = ControlUtils.GetInputHashCode(input); + return original != current; } } protected bool InputIsNotDefault(Control input) { if (input is UnitTextBox utbx) input = utbx.TextBox; - if (!DefaultValues.ContainsKey(input)) { + if (!DefaultValues.TryGetValue(input, out int? defaultValue)) { return false; - } else if (input is TextBox tb) { - return DefaultValues[tb] != Utils.GetEntityIdentifier(tb.Text); - } else if (input is ComboBox sb) { - return DefaultValues[sb] != Utils.GetEntityIdentifier(sb.SelectedItem); - } else if (input is CheckComboBox ccb) { - return DefaultValues[ccb] != Utils.GetEntityIdentifier(ccb.SelectedItems); - } else if (input is CheckBox cb) { - return DefaultValues[cb] != Utils.GetEntityIdentifier(cb.IsChecked); - } else if (input is RadioButton rb) { - return DefaultValues[rb] != Utils.GetEntityIdentifier(rb.IsChecked); } else { - return false; + var current = ControlUtils.GetInputHashCode(input); + return defaultValue != current; } } @@ -343,7 +325,7 @@ namespace Elwig.Windows { .ToListAsync(); } - ControlUtils.RenewItemsSource(ortInput, list, i => (i as AT_PlzDest)?.Id); + ControlUtils.RenewItemsSource(ortInput, list); if (list != null && ortInput.SelectedItem == null && list.Count == 1) ortInput.SelectedItem = list[0]; UpdateComboBox(ortInput); diff --git a/Elwig/Windows/AreaComAdminWindow.xaml.cs b/Elwig/Windows/AreaComAdminWindow.xaml.cs index 4502754..3910981 100644 --- a/Elwig/Windows/AreaComAdminWindow.xaml.cs +++ b/Elwig/Windows/AreaComAdminWindow.xaml.cs @@ -61,7 +61,7 @@ namespace Elwig.Windows { .ToList(); } - ControlUtils.RenewItemsSource(AreaCommitmentList, areaComs, i => (i as AreaCom)?.FbNr, + ControlUtils.RenewItemsSource(AreaCommitmentList, areaComs, AreaCommitmentList_SelectionChanged, filter.Count > 0 ? ControlUtils.RenewSourceDefault.IfOnly : ControlUtils.RenewSourceDefault.None, !updateSort); RefreshInputs(); @@ -153,18 +153,18 @@ namespace Elwig.Windows { YearFromInput.Text = a.YearFrom.ToString(); YearToInput.Text = a.YearTo.ToString(); - ControlUtils.SelectComboBoxItem(KgInput, k => (k as AT_Kg)?.KgNr, a.KgNr); + ControlUtils.SelectItemWithPk(KgInput, a.KgNr); if (a.RdNr != null) { - ControlUtils.SelectComboBoxItem(RdInput, r => (r as WbRd)?.RdNr, a.RdNr); + ControlUtils.SelectItemWithPk(RdInput, a.KgNr, a.RdNr); } else { RdInput.SelectedIndex = 0; } GstNrInput.Text = a.GstNr; AreaInput.Text = a.Area.ToString(); - ControlUtils.SelectComboBoxItem(AreaComTypeInput, t => (t as AreaComType)?.VtrgId, a.VtrgId); + ControlUtils.SelectItemWithPk(AreaComTypeInput, a.VtrgId); if (a.CultId != null) { - ControlUtils.SelectComboBoxItem(WineCultivationInput, c => (c as WineCult)?.CultId, a.CultId); + ControlUtils.SelectItemWithPk(WineCultivationInput, a.CultId); } else { WineCultivationInput.SelectedIndex = 0; } @@ -196,17 +196,17 @@ namespace Elwig.Windows { .Include(k => k.AtKg.WbKg!.Rds) .Select(k => k.AtKg) .OrderBy(k => k.Name) - .ToListAsync(), i => (i as AT_Kg)?.KgNr); + .ToListAsync()); ControlUtils.RenewItemsSource(AreaComTypeInput, await ctx.AreaCommitmentTypes .Include(c => c.WineVar) .Include(c => c.WineAttr) .OrderBy(v => v.VtrgId) - .ToListAsync(), i => (i as AreaComType)?.VtrgId); + .ToListAsync()); var cultList = await ctx.WineCultivations .OrderBy(c => c.Name) .Cast().ToListAsync(); cultList.Insert(0, new NullItem()); - ControlUtils.RenewItemsSource(WineCultivationInput, cultList, i => (i as WineCult)?.CultId, null, ControlUtils.RenewSourceDefault.First); + ControlUtils.RenewItemsSource(WineCultivationInput, cultList, null, ControlUtils.RenewSourceDefault.First); await RefreshAreaCommitmentList(); } @@ -312,7 +312,7 @@ namespace Elwig.Windows { await RefreshAreaCommitmentList(); RefreshInputs(); SearchInput.Text = ""; - AreaCommitmentList.SelectedItem = AreaCommitmentList.ItemsSource.Cast().Where(a => a.FbNr == fbnr).FirstOrDefault(); + ControlUtils.SelectItem(AreaCommitmentList, AreaCommitmentList.ItemsSource.Cast().Where(a => a.FbNr == fbnr).FirstOrDefault()); } private void AreaCommitmentResetButton_Click(object sender, RoutedEventArgs evt) { @@ -418,10 +418,10 @@ namespace Elwig.Windows { if (KgInput.SelectedItem is AT_Kg kg) { var rdList = kg.WbKg!.Rds.OrderBy(r => r.Name).Cast().ToList(); rdList.Insert(0, new NullItem()); - ControlUtils.RenewItemsSource(RdInput, rdList, i => (i as WbRd)?.RdNr, null, ControlUtils.RenewSourceDefault.First); + ControlUtils.RenewItemsSource(RdInput, rdList, null, ControlUtils.RenewSourceDefault.First); } else { var rdList = new object[] { new NullItem() }; - ControlUtils.RenewItemsSource(RdInput, rdList, i => (i as WbRd)?.RdNr, null, ControlUtils.RenewSourceDefault.First); + ControlUtils.RenewItemsSource(RdInput, rdList, null, ControlUtils.RenewSourceDefault.First); } ComboBox_SelectionChanged(sender, evt); } diff --git a/Elwig/Windows/BaseDataWindow.xaml.AreaCom.cs b/Elwig/Windows/BaseDataWindow.xaml.AreaCom.cs index 575b5bf..9dc1349 100644 --- a/Elwig/Windows/BaseDataWindow.xaml.AreaCom.cs +++ b/Elwig/Windows/BaseDataWindow.xaml.AreaCom.cs @@ -26,12 +26,12 @@ namespace Elwig.Windows { .ToListAsync()); _acts = _actList.ToDictionary(v => v.VtrgId, v => (string?)v.VtrgId); _actIds = _actList.ToDictionary(v => v, v => v.VtrgId); - ControlUtils.RenewItemsSource(AreaCommitmentTypeList, _actList, a => (a as AreaComType)?.VtrgId); + ControlUtils.RenewItemsSource(AreaCommitmentTypeList, _actList); AreaCommitmentTypeList_SelectionChanged(null, null); } private async Task AreaCommitmentTypesFinishEditing(AppDbContext ctx) { - ControlUtils.RenewItemsSource(AreaCommitmentTypeList, await ctx.AreaCommitmentTypes.OrderBy(v => v.SortId).ToListAsync(), v => (v as AreaComType)?.VtrgId); + ControlUtils.RenewItemsSource(AreaCommitmentTypeList, await ctx.AreaCommitmentTypes.OrderBy(v => v.SortId).ToListAsync()); _actList = null; _acts = null; _actIds = null; @@ -73,8 +73,8 @@ namespace Elwig.Windows { _actUpdate = true; if (AreaCommitmentTypeList.SelectedItem is AreaComType type) { AreaCommitmentTypeIdInput.Text = $"{type.SortId}{type.AttrId}"; - ControlUtils.SelectComboBoxItem(AreaCommitmentTypeWineVariantInput, s => (s as WineVar)?.SortId, type.SortId); - ControlUtils.SelectComboBoxItem(AreaCommitmentTypeWineAttributeInput, a => (a as WineAttr)?.AttrId, type.AttrId); + ControlUtils.SelectItemWithPk(AreaCommitmentTypeWineVariantInput, type.SortId); + ControlUtils.SelectItemWithPk(AreaCommitmentTypeWineAttributeInput, type.AttrId); AreaCommitmentTypeMinKgPerHaInput.Text = $"{type.MinKgPerHa}"; AreaCommitmentTypePenaltyPerKgInput.Text = $"{type.PenaltyPerKg}"; AreaCommitmentTypePenaltyInput.Text = $"{type.PenaltyAmount}"; diff --git a/Elwig/Windows/BaseDataWindow.xaml.Branch.cs b/Elwig/Windows/BaseDataWindow.xaml.Branch.cs index bb2ad6c..f3fa535 100644 --- a/Elwig/Windows/BaseDataWindow.xaml.Branch.cs +++ b/Elwig/Windows/BaseDataWindow.xaml.Branch.cs @@ -25,7 +25,7 @@ namespace Elwig.Windows { .ToListAsync()); _branches = _branchList.ToDictionary(b => b.ZwstId, b => (string?)b.ZwstId); _branchIds = _branchList.ToDictionary(b => b, b => b.ZwstId); - ControlUtils.RenewItemsSource(BranchList, _branchList, b => (b as Branch)?.ZwstId); + ControlUtils.RenewItemsSource(BranchList, _branchList); BranchList_SelectionChanged(null, null); } @@ -33,7 +33,7 @@ namespace Elwig.Windows { ControlUtils.RenewItemsSource(BranchList, await ctx.Branches .OrderBy(b => b.Name) .Include(b => b.PostalDest!.AtPlz) - .ToListAsync(), b => (b as Branch)?.ZwstId); + .ToListAsync()); _branchList = null; _branches = null; _branchIds = null; @@ -85,7 +85,7 @@ namespace Elwig.Windows { BranchIdInput.Text = branch.ZwstId; BranchNameInput.Text = branch.Name; BranchPlzInput.Text = branch.PostalDest?.AtPlz?.Plz.ToString() ?? ""; - ControlUtils.SelectComboBoxItem(BranchOrtInput, o => (o as AT_PlzDest)?.Okz, branch.PostalDest?.AtPlz?.Okz); + ControlUtils.SelectItem(BranchOrtInput, branch.PostalDest?.AtPlz); BranchAddressInput.Text = branch.Address; BranchPhoneNrInput.Text = branch.PhoneNr; BranchFaxNrInput.Text = branch.FaxNr; diff --git a/Elwig/Windows/BaseDataWindow.xaml.Mod.cs b/Elwig/Windows/BaseDataWindow.xaml.Mod.cs index 5685813..6636aea 100644 --- a/Elwig/Windows/BaseDataWindow.xaml.Mod.cs +++ b/Elwig/Windows/BaseDataWindow.xaml.Mod.cs @@ -25,13 +25,13 @@ namespace Elwig.Windows { _modList = new(await ctx.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToListAsync()); _mods = _modList.ToDictionary(m => m.ModId, m => (string?)m.ModId); _modIds = _modList.ToDictionary(m => m, m => m.ModId); - ControlUtils.RenewItemsSource(SeasonModifierList, _modList, m => (m as Modifier)?.ModId); + ControlUtils.RenewItemsSource(SeasonModifierList, _modList); SeasonModifierList_SelectionChanged(null, null); } private async Task ModifiersFinishEditing(AppDbContext ctx) { var year = (SeasonList.SelectedItem as Season)?.Year; - ControlUtils.RenewItemsSource(SeasonModifierList, await ctx.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToListAsync(), m => (m as Modifier)?.ModId); + ControlUtils.RenewItemsSource(SeasonModifierList, await ctx.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToListAsync()); _modList = null; _mods = null; _modIds = null; diff --git a/Elwig/Windows/BaseDataWindow.xaml.Season.cs b/Elwig/Windows/BaseDataWindow.xaml.Season.cs index 2e8d4f1..53d14c2 100644 --- a/Elwig/Windows/BaseDataWindow.xaml.Season.cs +++ b/Elwig/Windows/BaseDataWindow.xaml.Season.cs @@ -13,12 +13,12 @@ namespace Elwig.Windows { private bool _seasonUpdate = false; private async Task SeasonsInitEditing(AppDbContext ctx) { - ControlUtils.RenewItemsSource(SeasonList, await ctx.Seasons.OrderByDescending(s => s.Year).ToListAsync(), s => (s as Season)?.Year); + ControlUtils.RenewItemsSource(SeasonList, await ctx.Seasons.OrderByDescending(s => s.Year).ToListAsync()); SeasonList_SelectionChanged(null, null); } private async Task SeasonsFinishEditing(AppDbContext ctx) { - ControlUtils.RenewItemsSource(SeasonList, await ctx.Seasons.OrderByDescending(s => s.Year).Include(s => s.Modifiers).ToListAsync(), s => (s as Season)?.Year); + ControlUtils.RenewItemsSource(SeasonList, await ctx.Seasons.OrderByDescending(s => s.Year).Include(s => s.Modifiers).ToListAsync()); _seasonChanged = false; } diff --git a/Elwig/Windows/BaseDataWindow.xaml.WineAttr.cs b/Elwig/Windows/BaseDataWindow.xaml.WineAttr.cs index 2375979..532a24e 100644 --- a/Elwig/Windows/BaseDataWindow.xaml.WineAttr.cs +++ b/Elwig/Windows/BaseDataWindow.xaml.WineAttr.cs @@ -22,12 +22,12 @@ namespace Elwig.Windows { _attrList = new(await ctx.WineAttributes.OrderBy(a => a.Name).ToListAsync()); _attrs = _attrList.ToDictionary(a => a.AttrId, a => (string?)a.AttrId); _attrIds = _attrList.ToDictionary(a => a, a => a.AttrId); - ControlUtils.RenewItemsSource(WineAttributeList, _attrList, a => (a as WineAttr)?.AttrId); + ControlUtils.RenewItemsSource(WineAttributeList, _attrList); WineAttributeList_SelectionChanged(null, null); } private async Task WineAttributesFinishEditing(AppDbContext ctx) { - ControlUtils.RenewItemsSource(WineAttributeList, await ctx.WineAttributes.OrderBy(a => a.Name).ToListAsync(), a => (a as WineAttr)?.AttrId); + ControlUtils.RenewItemsSource(WineAttributeList, await ctx.WineAttributes.OrderBy(a => a.Name).ToListAsync()); _attrList = null; _attrs = null; _attrIds = null; diff --git a/Elwig/Windows/BaseDataWindow.xaml.WineCult.cs b/Elwig/Windows/BaseDataWindow.xaml.WineCult.cs index d538bbf..46b2586 100644 --- a/Elwig/Windows/BaseDataWindow.xaml.WineCult.cs +++ b/Elwig/Windows/BaseDataWindow.xaml.WineCult.cs @@ -22,12 +22,12 @@ namespace Elwig.Windows { _cultList = new(await ctx.WineCultivations.OrderBy(c => c.Name).ToListAsync()); _cults = _cultList.ToDictionary(c => c.CultId, c => (string?)c.CultId); _cultIds = _cultList.ToDictionary(c => c, c => c.CultId); - ControlUtils.RenewItemsSource(WineCultivationList, _cultList, c => (c as WineCult)?.CultId); + ControlUtils.RenewItemsSource(WineCultivationList, _cultList); WineCultivationList_SelectionChanged(null, null); } private async Task WineCultivationsFinishEditing(AppDbContext ctx) { - ControlUtils.RenewItemsSource(WineCultivationList, await ctx.WineCultivations.OrderBy(c => c.Name).ToListAsync(), c => (c as WineCult)?.CultId); + ControlUtils.RenewItemsSource(WineCultivationList, await ctx.WineCultivations.OrderBy(c => c.Name).ToListAsync()); _cultList = null; _cults = null; _cultIds = null; diff --git a/Elwig/Windows/BaseDataWindow.xaml.cs b/Elwig/Windows/BaseDataWindow.xaml.cs index 0ef3a10..ab8d084 100644 --- a/Elwig/Windows/BaseDataWindow.xaml.cs +++ b/Elwig/Windows/BaseDataWindow.xaml.cs @@ -137,33 +137,33 @@ namespace Elwig.Windows { FillInputs(App.Client); ControlUtils.RenewItemsSource(SeasonList, await ctx.Seasons .OrderByDescending(s => s.Year) - .ToListAsync(), s => (s as Season)?.Year, null, ControlUtils.RenewSourceDefault.First); + .ToListAsync(), null, ControlUtils.RenewSourceDefault.First); var year = (SeasonList.SelectedItem as Season)?.Year; ControlUtils.RenewItemsSource(BranchList, await ctx.Branches .OrderBy(b => b.Name) .Include(b => b.PostalDest!.AtPlz) - .ToListAsync(), b => (b as Branch)?.ZwstId, null, ControlUtils.RenewSourceDefault.First); + .ToListAsync(), null, ControlUtils.RenewSourceDefault.First); ControlUtils.RenewItemsSource(WineAttributeList, await ctx.WineAttributes .OrderBy(a => a.Name) - .ToListAsync(), a => (a as WineAttr)?.AttrId, null, ControlUtils.RenewSourceDefault.First); + .ToListAsync(), null, ControlUtils.RenewSourceDefault.First); ControlUtils.RenewItemsSource(AreaCommitmentTypeWineVariantInput, await ctx.WineVarieties .OrderBy(s => s.Name) - .ToListAsync(), s => (s as WineVar)?.SortId); + .ToListAsync()); var attrList = await ctx.WineAttributes.OrderBy(a => a.Name).Cast().ToListAsync(); attrList.Insert(0, new NullItem("")); - ControlUtils.RenewItemsSource(AreaCommitmentTypeWineAttributeInput, attrList, a => (a as WineAttr)?.AttrId); + ControlUtils.RenewItemsSource(AreaCommitmentTypeWineAttributeInput, attrList); ControlUtils.RenewItemsSource(AreaCommitmentTypeList, await ctx.AreaCommitmentTypes .OrderBy(t => t.VtrgId) .Include(t => t.WineVar) .Include(t => t.WineAttr) - .ToListAsync(), t => (t as AreaComType)?.VtrgId, null, ControlUtils.RenewSourceDefault.First); + .ToListAsync(), null, ControlUtils.RenewSourceDefault.First); ControlUtils.RenewItemsSource(WineCultivationList, await ctx.WineCultivations .OrderBy(c => c.Name) - .ToListAsync(), c=> (c as WineCult)?.CultId, null, ControlUtils.RenewSourceDefault.First); + .ToListAsync(), null, ControlUtils.RenewSourceDefault.First); ControlUtils.RenewItemsSource(SeasonModifierList, await ctx.Modifiers .Where(m => m.Year == year) .OrderBy(m => m.Ordering) - .ToListAsync(), m => (m as Modifier)?.ModId, null, ControlUtils.RenewSourceDefault.First); + .ToListAsync(), null, ControlUtils.RenewSourceDefault.First); } protected override void UpdateButtons() { diff --git a/Elwig/Windows/ChartWindow.xaml.cs b/Elwig/Windows/ChartWindow.xaml.cs index cdbe960..1935b53 100644 --- a/Elwig/Windows/ChartWindow.xaml.cs +++ b/Elwig/Windows/ChartWindow.xaml.cs @@ -139,9 +139,9 @@ namespace Elwig.Windows { }); FillingInputs = true; - ControlUtils.RenewItemsSource(VaributeInput, Vaributes, v => (v as Varibute)?.Listing); + ControlUtils.RenewItemsSource(VaributeInput, Vaributes); FillingInputs = false; - ControlUtils.RenewItemsSource(GraphList, GraphEntries, g => (g as GraphEntry)?.VaributeStringChange, GraphList_SelectionChanged, ControlUtils.RenewSourceDefault.First); + ControlUtils.RenewItemsSource(GraphList, GraphEntries, GraphList_SelectionChanged, ControlUtils.RenewSourceDefault.First); RefreshInputs(); } @@ -194,7 +194,7 @@ namespace Elwig.Windows { GebundenFlatBonus.Text = ""; } - ControlUtils.SelectCheckComboBoxItems(VaributeInput, SelectedGraphEntry?.Vaributes ?? [], i => (i as Varibute)?.Listing); + ControlUtils.SelectItems(VaributeInput, SelectedGraphEntry?.Vaributes ?? []); InitPlot(); OechslePricePlot.IsEnabled = true; diff --git a/Elwig/Windows/DeliveryAdminWindow.xaml b/Elwig/Windows/DeliveryAdminWindow.xaml index f059fbf..37e39e3 100644 --- a/Elwig/Windows/DeliveryAdminWindow.xaml +++ b/Elwig/Windows/DeliveryAdminWindow.xaml @@ -396,10 +396,10 @@ + Checked="LesewagenInput_Changed" Unchecked="LesewagenInput_Changed" Click="HandPickedInput_Changed"/> + Checked="HandPickedInput_Changed" Unchecked="HandPickedInput_Changed" Click="HandPickedInput_Changed"/> diff --git a/Elwig/Windows/DeliveryAdminWindow.xaml.cs b/Elwig/Windows/DeliveryAdminWindow.xaml.cs index 642f5a3..31a7da0 100644 --- a/Elwig/Windows/DeliveryAdminWindow.xaml.cs +++ b/Elwig/Windows/DeliveryAdminWindow.xaml.cs @@ -288,7 +288,7 @@ namespace Elwig.Windows { } private void InitInputs() { - ControlUtils.SelectComboBoxItem(BranchInput, i => (i as Branch)?.ZwstId, App.ZwstId); + ControlUtils.SelectItemWithPk(BranchInput, App.ZwstId); OnSecondPassed(null, null); UpdateLsNr().GetAwaiter().GetResult(); InitialInputs(); @@ -646,7 +646,7 @@ namespace Elwig.Windows { } deliveries.ForEach(d => { d.PartFilter = predicate; }); - ControlUtils.RenewItemsSource(DeliveryList, deliveries, d => ((d as Delivery)?.Year, (d as Delivery)?.DId), + ControlUtils.RenewItemsSource(DeliveryList, deliveries, DeliveryList_SelectionChanged, filter.Count > 0 ? ControlUtils.RenewSourceDefault.IfOnly : ControlUtils.RenewSourceDefault.None, !updateSort); await RefreshDeliveryParts(); @@ -795,21 +795,21 @@ namespace Elwig.Windows { await RefreshDeliveryList(); var d = DeliveryList.SelectedItem as Delivery; var y = d?.Year ?? Utils.CurrentLastSeason; - ControlUtils.RenewItemsSource(MemberInput, await Context.Members.Where(m => m.IsActive || !IsCreating).OrderBy(m => m.FamilyName).ThenBy(m => m.GivenName).ToListAsync(), i => (i as Member)?.MgNr); - ControlUtils.RenewItemsSource(BranchInput, await Context.Branches.OrderBy(b => b.Name).ToListAsync(), i => (i as Branch)?.ZwstId); - ControlUtils.RenewItemsSource(WineVarietyInput, await Context.WineVarieties.OrderBy(v => v.Name).ToListAsync(), i => (i as WineVar)?.SortId); + ControlUtils.RenewItemsSource(MemberInput, await Context.Members.Where(m => m.IsActive || !IsCreating).OrderBy(m => m.FamilyName).ThenBy(m => m.GivenName).ToListAsync()); + ControlUtils.RenewItemsSource(BranchInput, await Context.Branches.OrderBy(b => b.Name).ToListAsync()); + ControlUtils.RenewItemsSource(WineVarietyInput, await Context.WineVarieties.OrderBy(v => v.Name).ToListAsync()); var attrList = await Context.WineAttributes.Where(a => !IsCreating || a.IsActive).OrderBy(a => a.Name).Cast().ToListAsync(); attrList.Insert(0, new NullItem("")); - ControlUtils.RenewItemsSource(AttributeInput, attrList, i => (i as WineAttr)?.AttrId, null, ControlUtils.RenewSourceDefault.First); + ControlUtils.RenewItemsSource(AttributeInput, attrList, null, ControlUtils.RenewSourceDefault.First); var cultList = await Context.WineCultivations.OrderBy(a => a.Name).Cast().ToListAsync(); cultList.Insert(0, new NullItem("")); - ControlUtils.RenewItemsSource(CultivationInput, cultList, i => (i as WineCult)?.CultId, null, ControlUtils.RenewSourceDefault.First); - ControlUtils.RenewItemsSource(WineQualityLevelInput, await Context.WineQualityLevels.ToListAsync(), i => (i as WineQualLevel)?.QualId); - ControlUtils.RenewItemsSource(ModifiersInput, await Context.Modifiers.Where(m => m.Year == y).OrderBy(m => m.Ordering).ToListAsync(), i => (i as Modifier)?.ModId); - ControlUtils.RenewItemsSource(WineOriginInput, (await Context.WineOrigins.ToListAsync()).OrderByDescending(o => o.SortKey).ThenBy(o => o.HkId), i => (i as WineOrigin)?.HkId); + ControlUtils.RenewItemsSource(CultivationInput, cultList, null, ControlUtils.RenewSourceDefault.First); + ControlUtils.RenewItemsSource(WineQualityLevelInput, await Context.WineQualityLevels.ToListAsync()); + ControlUtils.RenewItemsSource(ModifiersInput, await Context.Modifiers.Where(m => m.Year == y).OrderBy(m => m.Ordering).ToListAsync()); + ControlUtils.RenewItemsSource(WineOriginInput, (await Context.WineOrigins.ToListAsync()).OrderByDescending(o => o.SortKey).ThenBy(o => o.HkId)); var kgList = await Context.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).Cast().ToListAsync(); kgList.Insert(0, new NullItem()); - ControlUtils.RenewItemsSource(WineKgInput, kgList, i => (i as AT_Kg)?.KgNr); + ControlUtils.RenewItemsSource(WineKgInput, kgList); UpdateRdInput(); if (IsCreating) await UpdateLsNr(); @@ -826,10 +826,10 @@ namespace Elwig.Windows { private async Task RefreshDeliveryParts() { if (DeliveryList.SelectedItem is Delivery d) { - ControlUtils.RenewItemsSource(ModifiersInput, await Context.Modifiers.Where(m => m.Year == d.Year).OrderBy(m => m.Ordering).ToListAsync(), i => (i as Modifier)?.ModId); - ControlUtils.RenewItemsSource(DeliveryPartList, d.FilteredParts.OrderBy(p => p.DPNr).ToList(), i => ((i as DeliveryPart)?.Year, (i as DeliveryPart)?.DId, (i as DeliveryPart)?.DPNr), DeliveryPartList_SelectionChanged, ControlUtils.RenewSourceDefault.First); + ControlUtils.RenewItemsSource(ModifiersInput, await Context.Modifiers.Where(m => m.Year == d.Year).OrderBy(m => m.Ordering).ToListAsync()); + ControlUtils.RenewItemsSource(DeliveryPartList, d.FilteredParts.OrderBy(p => p.DPNr).ToList(), DeliveryPartList_SelectionChanged, ControlUtils.RenewSourceDefault.First); } else { - ControlUtils.RenewItemsSource(ModifiersInput, await Context.Modifiers.Where(m => m.Year == Utils.CurrentLastSeason).OrderBy(m => m.Ordering).ToListAsync(), i => (i as Modifier)?.ModId); + ControlUtils.RenewItemsSource(ModifiersInput, await Context.Modifiers.Where(m => m.Year == Utils.CurrentLastSeason).OrderBy(m => m.Ordering).ToListAsync()); DeliveryPartList.ItemsSource = null; } } @@ -854,7 +854,7 @@ namespace Elwig.Windows { ClearDefaultValues(); MgNrInput.Text = d.MgNr.ToString(); - ControlUtils.SelectComboBoxItem(BranchInput, i => (i as Branch)?.ZwstId, d.ZwstId); + ControlUtils.SelectItemWithPk(BranchInput, d.ZwstId); LsNrInput.Text = d.LsNr; DateInput.Text = d.Date.ToString("dd.MM.yyyy"); TimeInput.Text = d.Time?.ToString("HH:mm") ?? ""; @@ -877,17 +877,17 @@ namespace Elwig.Windows { ClearDefaultValues(); SortIdInput.Text = p?.SortId ?? ""; - ControlUtils.SelectComboBoxItem(AttributeInput, p?.Attribute, i => (i as WineAttr)?.AttrId); - ControlUtils.SelectComboBoxItem(CultivationInput, p?.Cultivation, i => (i as WineCult)?.CultId); + ControlUtils.SelectItemWithPk(AttributeInput, p?.AttrId); + ControlUtils.SelectItemWithPk(CultivationInput, p?.CultId); GradationKmwInput.Text = (p != null) ? $"{p.Kmw:N1}" : ""; - ControlUtils.SelectComboBoxItem(WineQualityLevelInput, q => (q as WineQualLevel)?.QualId, p?.QualId); - ControlUtils.SelectComboBoxItem(WineKgInput, k => (k as AT_Kg)?.KgNr, p?.KgNr); - ControlUtils.SelectComboBoxItem(WineRdInput, r => (r as WbRd)?.RdNr, p?.RdNr); - ControlUtils.SelectComboBoxItem(WineOriginInput, r => (r as WineOrigin)?.HkId, p?.HkId); + ControlUtils.SelectItemWithPk(WineQualityLevelInput, p?.QualId); + ControlUtils.SelectItemWithPk(WineKgInput, p?.KgNr); + ControlUtils.SelectItemWithPk(WineRdInput, p?.KgNr, p?.RdNr); + ControlUtils.SelectItemWithPk(WineOriginInput, p?.HkId); WeightInput.Text = (p != null) ? $"{p.Weight:N0}" : ""; ManualWeighingInput.IsChecked = p?.IsManualWeighing ?? false; GerebeltGewogenInput.IsChecked = p?.IsNetWeight ?? false; - ControlUtils.SelectCheckComboBoxItems(ModifiersInput, p?.Modifiers, i => (i as Modifier)?.ModId); + ControlUtils.SelectItems(ModifiersInput, p?.Modifiers); PartCommentInput.Text = p?.Comment ?? ""; TemperatureInput.Text = (p != null && p.Temperature != null) ? $"{p.Temperature:N1}" : ""; AcidInput.Text = (p != null && p.Acid != null) ? $"{p.Acid:N1}" : ""; @@ -1118,12 +1118,12 @@ namespace Elwig.Windows { private void MgNrInput_TextChanged(object sender, TextChangedEventArgs evt) { var valid = InputTextChanged((TextBox)sender, Validator.CheckMgNr); - MemberInput.SelectedItem = valid ? Context.Members.Find(int.Parse(MgNrInput.Text)) : null; + ControlUtils.SelectItemWithPk(MemberInput, valid ? int.Parse(MgNrInput.Text) : null); } private void MgNrInput_LostFocus(object sender, RoutedEventArgs evt) { var valid = InputLostFocus((TextBox)sender, Validator.CheckMgNr); - MemberInput.SelectedItem = valid ? Context.Members.Find(int.Parse(MgNrInput.Text)) : null; + ControlUtils.SelectItemWithPk(MemberInput, valid ? int.Parse(MgNrInput.Text) : null); } private void MemberInput_SelectionChanged(object? sender, SelectionChangedEventArgs? evt) { @@ -1134,8 +1134,8 @@ namespace Elwig.Windows { UnsetDefaultValue(WineKgInput); WineKgInput.SelectedIndex = 0; } else { - SetDefaultValue(WineKgInput, m.DefaultKg); - WineKgInput.SelectedItem = m.DefaultKg; + ControlUtils.SelectItemWithPk(WineKgInput, m.DefaultKgNr); + SetDefaultValue(WineKgInput); } } @@ -1155,7 +1155,7 @@ namespace Elwig.Windows { await RefreshDeliveryList(); await RefreshDeliveryParts(); NewDeliveryPartButton.Cursor = null; - DeliveryList.SelectedItem = p?.Delivery; + ControlUtils.SelectItem(DeliveryList, p?.Delivery); DeliveryPartList.SelectedItem = null; RefreshInputs(); InitialInputs(); @@ -1201,8 +1201,8 @@ namespace Elwig.Windows { var attrList = await Context.WineAttributes.OrderBy(a => a.Name).Cast().ToListAsync(); attrList.Insert(0, new NullItem("")); - ControlUtils.RenewItemsSource(AttributeInput, attrList, i => (i as WineAttr)?.AttrId, null, ControlUtils.RenewSourceDefault.First); - ControlUtils.RenewItemsSource(MemberInput, await Context.Members.Where(m => m.IsActive || !IsReceipt).OrderBy(m => m.FamilyName).ThenBy(m => m.GivenName).ToListAsync(), i => (i as Member)?.MgNr); + ControlUtils.RenewItemsSource(AttributeInput, attrList, null, ControlUtils.RenewSourceDefault.First); + ControlUtils.RenewItemsSource(MemberInput, await Context.Members.Where(m => m.IsActive || !IsReceipt).OrderBy(m => m.FamilyName).ThenBy(m => m.GivenName).ToListAsync()); if (DeliveryList.SelectedItem is not Delivery d) { // switch away from creating mode IsCreating = false; @@ -1220,7 +1220,7 @@ namespace Elwig.Windows { } else { // switch to last delivery part DeliveryPartList.IsEnabled = true; - DeliveryPartList.SelectedItem = d.FilteredParts.Last(); + ControlUtils.SelectItem(DeliveryPartList, d.FilteredParts.Last()); } } @@ -1229,8 +1229,8 @@ namespace Elwig.Windows { SearchInput.Text = ""; var attrList = await Context.WineAttributes.Where(a => a.IsActive).OrderBy(a => a.Name).Cast().ToListAsync(); attrList.Insert(0, new NullItem("")); - ControlUtils.RenewItemsSource(AttributeInput, attrList, i => (i as WineAttr)?.AttrId, null, ControlUtils.RenewSourceDefault.First); - ControlUtils.RenewItemsSource(MemberInput, await Context.Members.Where(m => m.IsActive || !IsReceipt).OrderBy(m => m.FamilyName).ThenBy(m => m.GivenName).ToListAsync(), i => (i as Member)?.MgNr); + ControlUtils.RenewItemsSource(AttributeInput, attrList, null, ControlUtils.RenewSourceDefault.First); + ControlUtils.RenewItemsSource(MemberInput, await Context.Members.Where(m => m.IsActive || !IsReceipt).OrderBy(m => m.FamilyName).ThenBy(m => m.GivenName).ToListAsync()); IsCreating = true; DeliveryList.IsEnabled = false; DeliveryPartList.IsEnabled = false; @@ -1253,8 +1253,8 @@ namespace Elwig.Windows { Mouse.OverrideCursor = Cursors.AppStarting; ClearOriginalValues(); if (res >= p.Weight) { - ControlUtils.SelectComboBoxItem(WineQualityLevelInput, q => (q as WineQualLevel)?.QualId, "WEI"); - ControlUtils.SelectComboBoxItem(WineOriginInput, o => (o as WineOrigin)?.HkId, "OEST"); + ControlUtils.SelectItemWithPk(WineQualityLevelInput, "WEI"); + ControlUtils.SelectItemWithPk(WineOriginInput, "OEST"); p.QualId = "WEI"; p.HkId = "OEST"; entry1 = Context.Update(p); @@ -1449,7 +1449,7 @@ namespace Elwig.Windows { Mouse.OverrideCursor = null; await RefreshDeliveryList(); - DeliveryList.SelectedItem = d; + ControlUtils.SelectItem(DeliveryList, d); } catch (Exception exc) { if (entry != null) { entry.State = EntityState.Detached; @@ -1612,10 +1612,10 @@ namespace Elwig.Windows { private void UpdateWineVariety(bool valid) { if (valid) { var text = SortIdInput.Text; - WineVarietyInput.SelectedItem = Context.WineVarieties.Find(text[0..2]); + ControlUtils.SelectItemWithPk(WineVarietyInput, text[0..2]); if (text.Length >= 3) { - ControlUtils.SelectComboBoxItem(AttributeInput, Context.WineAttributes.Find(text[2..]), a => (a as WineAttr)?.AttrId); - ControlUtils.SelectComboBoxItem(CultivationInput, Context.WineCultivations.Find(text[2..]), i => (i as WineCult)?.CultId); + ControlUtils.SelectItemWithPk(AttributeInput, text[2..]); + ControlUtils.SelectItemWithPk(CultivationInput, text[2..]); SortIdInput.Text = text[0..2]; } } else { @@ -1649,7 +1649,7 @@ namespace Elwig.Windows { var qual = Context.GetWineQualityLevel(kmw).GetAwaiter().GetResult(); SetDefaultValue(WineQualityLevelInput, qual); if (WineQualityLevelInput.SelectedItem == null || (WineQualityLevelInput.SelectedItem is WineQualLevel selected && !selected.IsPredicate)) { - WineQualityLevelInput.SelectedItem = qual; + ControlUtils.SelectItem(WineQualityLevelInput, qual); } } @@ -1744,7 +1744,7 @@ namespace Elwig.Windows { var o = kg.Origin; while (o != null && o.Level > qual.OriginLevel) o = o.Parent; SetDefaultValue(WineOriginInput, o); - WineOriginInput.SelectedItem = o; + ControlUtils.SelectItem(WineOriginInput, o); } private void WineQualityLevelInput_SelectionChanged(object sender, SelectionChangedEventArgs evt) { @@ -1756,7 +1756,7 @@ namespace Elwig.Windows { if (WineKgInput.SelectedItem is AT_Kg kg) { var list = Context.WbRde.Where(r => r.KgNr == kg.KgNr).OrderBy(r => r.Name).Cast().ToList(); list.Insert(0, new NullItem()); - ControlUtils.RenewItemsSource(WineRdInput, list, i => ((i as WbRd)?.KgNr, (i as WbRd)?.RdNr)); + ControlUtils.RenewItemsSource(WineRdInput, list); if (WineRdInput.SelectedItem == null) WineRdInput.SelectedIndex = 0; WineRdInput.IsEnabled = (IsEditing || IsCreating) && list.Count > 1; } else { diff --git a/Elwig/Windows/MailWindow.xaml.cs b/Elwig/Windows/MailWindow.xaml.cs index ad48364..370d9f1 100644 --- a/Elwig/Windows/MailWindow.xaml.cs +++ b/Elwig/Windows/MailWindow.xaml.cs @@ -142,7 +142,7 @@ namespace Elwig.Windows { ControlUtils.RenewItemsSource(MemberBranchInput, await ctx.Branches .Where(b => b.Members.Any()) .OrderBy(b => b.Name) - .ToListAsync(), b => (b as Branch)?.ZwstId, MemberInput_SelectionChanged); + .ToListAsync(), MemberInput_SelectionChanged); if (MemberBranchInput.SelectedItems.Count == 0) { MemberBranchInput.ItemSelectionChanged -= MemberInput_SelectionChanged; MemberBranchInput.SelectAll(); @@ -151,7 +151,7 @@ namespace Elwig.Windows { ControlUtils.RenewItemsSource(MemberKgInput, await ctx.Katastralgemeinden .Where(k => k.WbKg!.Members.Any()) .OrderBy(k => k.Name) - .ToListAsync(), k => (k as AT_Kg)?.KgNr, MemberInput_SelectionChanged); + .ToListAsync(), MemberInput_SelectionChanged); if (MemberKgInput.SelectedItems.Count == 0) { MemberKgInput.ItemSelectionChanged -= MemberInput_SelectionChanged; MemberKgInput.SelectAll(); @@ -159,7 +159,7 @@ namespace Elwig.Windows { } ControlUtils.RenewItemsSource(MemberAreaComInput, await ctx.AreaCommitmentTypes .OrderBy(a => a.VtrgId) - .ToListAsync(), a => (a as AreaComType)?.VtrgId, MemberInput_SelectionChanged); + .ToListAsync(), MemberInput_SelectionChanged); if (MemberAreaComInput.SelectedItems.Count == 0) { MemberAreaComInput.ItemSelectionChanged -= MemberInput_SelectionChanged; MemberAreaComInput.SelectAll(); @@ -169,7 +169,7 @@ namespace Elwig.Windows { .Where(m => m.IsActive) .OrderBy(m => m.FamilyName) .ThenBy(m => m.GivenName) - .ToListAsync(), m => (m as Member)?.MgNr, MemberInput_SelectionChanged); + .ToListAsync(), MemberInput_SelectionChanged); if (MemberCustomInput.SelectedItems.Count == 0) { MemberCustomInput.ItemSelectionChanged -= MemberInput_SelectionChanged; MemberCustomInput.SelectAll(); diff --git a/Elwig/Windows/MemberAdminWindow.xaml.cs b/Elwig/Windows/MemberAdminWindow.xaml.cs index 602165b..d041591 100644 --- a/Elwig/Windows/MemberAdminWindow.xaml.cs +++ b/Elwig/Windows/MemberAdminWindow.xaml.cs @@ -90,8 +90,7 @@ namespace Elwig.Windows { FocusMember(mgnr); }); } else { - MemberList.SelectedItem = item; - MemberList.ScrollIntoView(MemberList.SelectedItem); + ControlUtils.SelectItem(MemberList, item); } } @@ -218,7 +217,7 @@ namespace Elwig.Windows { .ToList(); } - ControlUtils.RenewItemsSource(MemberList, members, i => (i as Member)?.MgNr, + ControlUtils.RenewItemsSource(MemberList, members, MemberList_SelectionChanged, TextFilter.Count > 0 ? ControlUtils.RenewSourceDefault.IfOnly : ControlUtils.RenewSourceDefault.None, !updateSort); } @@ -268,8 +267,8 @@ namespace Elwig.Windows { protected override async Task OnRenewContext() { await base.OnRenewContext(); using var ctx = new AppDbContext(); - ControlUtils.RenewItemsSource(BranchInput, await ctx.Branches.OrderBy(b => b.Name).ToListAsync(), i => (i as Branch)?.ZwstId); - ControlUtils.RenewItemsSource(DefaultKgInput, await ctx.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToListAsync(), i => (i as AT_Kg)?.KgNr); + ControlUtils.RenewItemsSource(BranchInput, await ctx.Branches.OrderBy(b => b.Name).ToListAsync()); + ControlUtils.RenewItemsSource(DefaultKgInput, await ctx.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToListAsync()); await RefreshMemberList(); StatusMembers.Text = $"Mitglieder: {await ctx.Members.CountAsync(m => m.IsActive):N0} ({await ctx.Members.CountAsync():N0})"; StatusBusinessShares.Text = $"Geschäftsanteile: {await ctx.Members.Where(m => m.IsActive).SumAsync(m => m.BusinessShares):N0} ({await ctx.Members.SumAsync(m => m.BusinessShares):N0})"; @@ -686,7 +685,7 @@ namespace Elwig.Windows { AT_PlzDest? p = m.PostalDest.AtPlz; if (p != null) { PlzInput.Text = p.Plz.ToString(); - ControlUtils.SelectComboBoxItem(OrtInput, o => (o as AT_PlzDest)?.Okz, p.Okz); + ControlUtils.SelectItem(OrtInput, p); } else { PlzInput.Text = null; OrtInput.SelectedItem = null; @@ -728,7 +727,7 @@ namespace Elwig.Windows { AT_PlzDest? b = billingAddr.PostalDest.AtPlz; if (b != null) { BillingPlzInput.Text = b.Plz.ToString(); - ControlUtils.SelectComboBoxItem(BillingOrtInput, o => (o as AT_PlzDest)?.Okz, b.Okz); + ControlUtils.SelectItem(BillingOrtInput, b); } } else { BillingNameInput.Text = ""; @@ -741,8 +740,8 @@ namespace Elwig.Windows { ExitDateInput.Text = (m.ExitDateString != null) ? string.Join(".", m.ExitDateString.Split("-").Reverse()) : null; BusinessSharesInput.Text = m.BusinessShares.ToString(); AccountingNrInput.Text = m.AccountingNr; - ControlUtils.SelectComboBoxItem(BranchInput, b => (b as Branch)?.ZwstId, m.ZwstId); - ControlUtils.SelectComboBoxItem(DefaultKgInput, k => (k as AT_Kg)?.KgNr, m.DefaultKgNr); + ControlUtils.SelectItemWithPk(BranchInput, m.ZwstId); + ControlUtils.SelectItemWithPk(DefaultKgInput, m.DefaultKgNr); CommentInput.Text = m.Comment; ActiveInput.IsChecked = m.IsActive; VollLieferantInput.IsChecked = m.IsVollLieferant; diff --git a/Elwig/Windows/OriginHierarchyWindow.xaml.cs b/Elwig/Windows/OriginHierarchyWindow.xaml.cs index ed19b96..90f7c59 100644 --- a/Elwig/Windows/OriginHierarchyWindow.xaml.cs +++ b/Elwig/Windows/OriginHierarchyWindow.xaml.cs @@ -28,7 +28,7 @@ namespace Elwig.Windows { .ToListAsync()) .OrderByDescending(o => o.SortKey) .ThenBy(o => o.HkId); - ControlUtils.RenewItemsSource(WineOrigins, origins, i => (i as WineOrigin)?.HkId, WineOrigins_SelectionChanged); + ControlUtils.RenewItemsSource(WineOrigins, origins, WineOrigins_SelectionChanged); if (WineOrigins.SelectedItem == null) { var hkid = await ctx.WbKgs .GroupBy(k => k.AtKg.Gem.WbGem!.HkId) @@ -36,14 +36,14 @@ namespace Elwig.Windows { .OrderByDescending(g => g.Count()) .Select(g => g.Key) .FirstOrDefaultAsync(); - ControlUtils.SelectListBoxItem(WineOrigins, o => (o as WineOrigin)?.HkId, hkid); + ControlUtils.SelectItemWithPk(WineOrigins, hkid); } var gls = await ctx.WbGls .OrderBy(g => g.GlNr) .Include("Kgs.Rds") .AsSplitQuery() .ToListAsync(); - ControlUtils.RenewItemsSource(WbGls, gls, g => (g as WbGl)?.GlNr, WbGls_SelectionChanged, ControlUtils.RenewSourceDefault.First); + ControlUtils.RenewItemsSource(WbGls, gls, WbGls_SelectionChanged, ControlUtils.RenewSourceDefault.First); } UpdateWbGems(); UpdateWbKgs(); @@ -55,7 +55,7 @@ namespace Elwig.Windows { WbGemsHeader.Content = "Gemeinden" + (WineOrigins.SelectedItem is WineOrigin o ? $" ({o.Name})" : ""); var origin = (WineOrigins.SelectedItem as WineOrigin); var gems = origin?.Gems.Select(g => g.AtGem).OrderBy(g => g.Name).ToList(); - ControlUtils.RenewItemsSource(WbGems, gems, g => (g as AT_Gem)?.Gkz, WbGems_SelectionChanged); + ControlUtils.RenewItemsSource(WbGems, gems, WbGems_SelectionChanged); UpdateWbKgs(); } @@ -72,7 +72,7 @@ namespace Elwig.Windows { kgs = null; } var kgList = kgs?.OrderBy(k => k.WbKg?.Gl == null).ThenBy(k => k.WbKg?.GlNr).ThenBy(k => k.Name).ToList(); - ControlUtils.RenewItemsSource(WbKgs, kgList, k => (k as AT_Kg)?.KgNr, WbKgs_SelectionChanged); + ControlUtils.RenewItemsSource(WbKgs, kgList, WbKgs_SelectionChanged); } private void UpdateWbGlKgs() { @@ -80,7 +80,7 @@ namespace Elwig.Windows { if (WbGls.SelectedItem is WbGl g) { WbGlKgsHeader.Content += $" ({g.Name})"; var kgs = g.Kgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToList(); - ControlUtils.RenewItemsSource(WbGlKgs, kgs, k => (k as AT_Kg)?.KgNr, WbGlKgs_SelectionChanged); + ControlUtils.RenewItemsSource(WbGlKgs, kgs, WbGlKgs_SelectionChanged); } else { WbGlKgs.ItemsSource = null; } @@ -122,7 +122,7 @@ namespace Elwig.Windows { if (WbGlKgs.SelectedItem is AT_Kg k) { WbRdsHeader.Content += $" ({k.Name})"; var rds = k.WbKg?.Rds.OrderBy(r => r.Name).ToList(); - ControlUtils.RenewItemsSource(WbRds, rds, k => (k as AT_Kg)?.KgNr); + ControlUtils.RenewItemsSource(WbRds, rds); } else { WbRds.ItemsSource = null; } @@ -145,9 +145,9 @@ namespace Elwig.Windows { UpdateWbRds(); if (!isUpdating && WbGlKgs.SelectedItem is AT_Kg k) { isUpdating = true; - ControlUtils.SelectListBoxItem(WineOrigins, o => (o as WineOrigin)?.HkId, k.Gem.WbGem?.HkId); - ControlUtils.SelectListBoxItem(WbGems, g => (g as AT_Gem)?.Gkz, k.Gkz); - ControlUtils.SelectListBoxItem(WbKgs, k => (k as AT_Kg)?.KgNr, k.KgNr); + ControlUtils.SelectItemWithPk(WineOrigins, k.Gem.WbGem?.HkId); + ControlUtils.SelectItemWithPk(WbGems, k.Gkz); + ControlUtils.SelectItemWithPk(WbKgs, k.KgNr); isUpdating = false; } await UpdateStatusBar(); @@ -157,8 +157,8 @@ namespace Elwig.Windows { UpdateButtons(); if (!isUpdating && WbKgs.SelectedItem is AT_Kg k && k.WbKg != null && ((WbGls.SelectedItem as WbGl)?.GlNr == k.WbKg?.GlNr || WbGls.SelectedItem == null)) { isUpdating = true; - ControlUtils.SelectListBoxItem(WbGls, g => (g as WbGl)?.GlNr, k.WbKg?.GlNr); - ControlUtils.SelectListBoxItem(WbGlKgs, k => (k as AT_Kg)?.KgNr, k.KgNr); + ControlUtils.SelectItemWithPk(WbGls, k.WbKg?.GlNr); + ControlUtils.SelectItemWithPk(WbGlKgs, k.KgNr); isUpdating = false; } } @@ -194,7 +194,7 @@ namespace Elwig.Windows { await ctx.SaveChangesAsync(); } await App.HintContextChange(); - ControlUtils.SelectListBoxItem(WbGlKgs, kg => (kg as AT_Kg)?.KgNr, k.KgNr); + ControlUtils.SelectItemWithPk(WbGlKgs, k.KgNr); } catch (Exception exc) { var str = "Der Eintrag konnte nicht in der Datenbank aktualisiert werden!\n\n" + exc.Message; if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message; @@ -214,7 +214,7 @@ namespace Elwig.Windows { await ctx.SaveChangesAsync(); } await App.HintContextChange(); - ControlUtils.SelectListBoxItem(WbKgs, kg => (kg as AT_Kg)?.KgNr, k.KgNr); + ControlUtils.SelectItemWithPk(WbKgs, k.KgNr); } catch (Exception exc) { await HintContextChange(); var str = "Der Eintrag konnte nicht aus der Datenbank gelöscht werden!\n\n" + exc.Message; @@ -226,8 +226,8 @@ namespace Elwig.Windows { public void FocusKgNr(int kgnr) { using var ctx = new AppDbContext(); var kg = ctx.Katastralgemeinden.Find(kgnr); - ControlUtils.SelectListBoxItem(WbGls, kg?.WbKg?.Gl, g => (g as WbGl)?.GlNr); - ControlUtils.SelectListBoxItem(WbGlKgs, kg, k => (k as AT_Kg)?.KgNr); + ControlUtils.SelectItemWithPk(WbGls, kg?.WbKg?.GlNr); + ControlUtils.SelectItemWithPk(WbGlKgs, kg?.KgNr); WbGlKgs.Focus(); WbGlKgs.ScrollIntoView(kg?.WbKg?.Gl); } diff --git a/Elwig/Windows/PaymentVariantsWindow.xaml.cs b/Elwig/Windows/PaymentVariantsWindow.xaml.cs index cc47a38..d10c177 100644 --- a/Elwig/Windows/PaymentVariantsWindow.xaml.cs +++ b/Elwig/Windows/PaymentVariantsWindow.xaml.cs @@ -42,7 +42,7 @@ namespace Elwig.Windows { .Where(v => v.Year == Year) .OrderBy(v => v.AvNr) .Include(v => v.Season.Currency) - .ToListAsync(), v => (v as PaymentVar)?.AvNr); + .ToListAsync()); await Update(); } @@ -207,7 +207,7 @@ namespace Elwig.Windows { await ctx.SaveChangesAsync(); await App.HintContextChange(); - ControlUtils.SelectListBoxItem(PaymentVariantList, v, v => (v as PaymentVar)?.AvNr); + ControlUtils.SelectItem(PaymentVariantList, v); } catch (Exception exc) { var str = "Der Eintrag konnte nicht in der Datenbank aktualisiert werden!\n\n" + exc.Message; if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message; @@ -232,7 +232,7 @@ namespace Elwig.Windows { await ctx.SaveChangesAsync(); await App.HintContextChange(); - ControlUtils.SelectListBoxItem(PaymentVariantList, n, v => (v as PaymentVar)?.AvNr); + ControlUtils.SelectItem(PaymentVariantList, n); } catch (Exception exc) { var str = "Der Eintrag konnte nicht in der Datenbank aktualisiert werden!\n\n" + exc.Message; if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message;