ControlUtils: Cleanup SelectItem() method and use accordingly

This commit is contained in:
2024-03-18 17:55:27 +01:00
parent 51e345f1fd
commit 2f3524db9d
20 changed files with 181 additions and 194 deletions

View File

@ -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);
}