Extract ControlUtils from Utils

This commit is contained in:
2023-07-21 18:31:57 +02:00
parent 5cee928978
commit fd7a15bc5a
7 changed files with 272 additions and 263 deletions

View File

@ -108,7 +108,7 @@ namespace Elwig {
protected void OnPrintingReadyChanged(EventArgs evt) {
foreach (Window w in Windows) {
foreach (var b in Utils.FindAllChildren<Button>(w).Where(b => "Print".Equals(b.Tag))) {
foreach (var b in ControlUtils.FindAllChildren<Button>(w).Where(b => "Print".Equals(b.Tag))) {
b.IsEnabled = IsPrintingReady;
}
}

View File

@ -0,0 +1,213 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
namespace Elwig.Helpers {
public class ControlUtils {
private static void SetControlBrush(Control input, Brush brush) {
if (input is ComboBox cb) {
var border = GetComboBoxBorder(cb);
if (border != null) border.BorderBrush = brush;
} else if (input is Xceed.Wpf.Toolkit.CheckComboBox ccb) {
var border = GetComboBoxBorder(ccb);
if (border != null) border.BorderBrush = brush;
} else {
input.BorderBrush = brush;
}
}
public static void SetInputChanged(Control input) {
SetControlBrush(input, Brushes.Orange);
}
public static void SetInputInvalid(Control input) {
SetControlBrush(input, Brushes.Red);
}
public static void ClearInputState(Control input) {
if (input is ComboBox cb) {
GetComboBoxBorder(cb)?.ClearValue(Border.BorderBrushProperty);
} else if (input is Xceed.Wpf.Toolkit.CheckComboBox ccb) {
GetComboBoxBorder(ccb)?.ClearValue(Border.BorderBrushProperty);
} else {
input.ClearValue(Control.BorderBrushProperty);
}
}
private static Border? GetComboBoxBorder(ComboBox cb) {
var toggleButton = cb.Template.FindName("toggleButton", cb) as ToggleButton;
return toggleButton?.Template.FindName("templateRoot", toggleButton) as Border;
}
private static Border? GetComboBoxBorder(Xceed.Wpf.Toolkit.CheckComboBox ccb) {
var toggleButton = ccb.Template.FindName("toggleButton", ccb) as ToggleButton;
return toggleButton?.Template.FindName("templateRoot", toggleButton) as Border;
}
public static IEnumerable<T> FindAllChildren<T>(DependencyObject depObj) where T : DependencyObject {
if (depObj == null)
yield return (T)Enumerable.Empty<T>();
foreach (var child in LogicalTreeHelper.GetChildren(depObj)) {
if (child == null) {
continue;
} else if (child is T t) {
yield return t;
} else if (child is DependencyObject childDepOpj) {
foreach (T childOfChild in FindAllChildren<T>(childDepOpj)) {
yield return childOfChild;
}
}
}
}
public static IEnumerable<T> FindAllChildren<T>(DependencyObject depObj, IEnumerable<DependencyObject> exempt) where T : DependencyObject {
return FindAllChildren<T>(depObj).Where(c => !exempt.Contains(c));
}
public static T? FindNextSibling<T>(Control me) where T : DependencyObject {
var found = false;
foreach (var child in LogicalTreeHelper.GetChildren(me.Parent)) {
if (found && child is T c) {
return c;
} else if (child == me) {
found = true;
}
}
return null;
}
public enum RenewSourceDefault {
None,
IfOnly,
First
}
public static void RenewItemsSource(Selector selector, IEnumerable? source, Func<object?, object?> getId, SelectionChangedEventHandler? handler = null, RenewSourceDefault def = RenewSourceDefault.None) {
if (selector.ItemsSource == source)
return;
var selectedId = getId(selector.SelectedItem);
if (handler != null) selector.SelectionChanged -= handler;
selector.ItemsSource = source;
if (selectedId != null && source != null)
selector.SelectedItem = source.Cast<object>().FirstOrDefault(i => selectedId.Equals(getId(i)));
if (source != null && selector.SelectedItem == null) {
if ((def == RenewSourceDefault.IfOnly && source.Cast<object>().Count() == 1) || def == RenewSourceDefault.First) {
selector.SelectedItem = source.Cast<object>().First();
}
}
if (handler != null) selector.SelectionChanged += handler;
}
public static void RenewItemsSource(Xceed.Wpf.Toolkit.Primitives.Selector selector, IEnumerable? source, Func<object?, object?> getId, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventHandler? handler = null, RenewSourceDefault def = RenewSourceDefault.None) {
if (selector.ItemsSource == source)
return;
var selectedIds = selector.SelectedItems.Cast<object>().Select(i => getId(i)).ToList();
if (handler != null) selector.ItemSelectionChanged -= handler;
selector.ItemsSource = source;
if (source != null) {
foreach (var i in source.Cast<object>().Where(i => selectedIds.Contains(getId(i))))
selector.SelectedItems.Add(i);
}
if (source != null && selector.SelectedItem == null) {
if ((def == RenewSourceDefault.IfOnly && source.Cast<object>().Count() == 1) || def == RenewSourceDefault.First) {
selector.SelectedItem = source.Cast<object>().First();
}
}
if (handler != null) selector.ItemSelectionChanged += handler;
}
public static void RenewItemsSource(DataGrid dataGrid, IEnumerable? source, Func<object?, object?> getId, 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);
if (handler != null) dataGrid.SelectionChanged -= handler;
dataGrid.ItemsSource = source;
if (keepSort) {
for (int i = 0; i < dataGrid.Columns.Count; i++)
dataGrid.Columns[i].SortDirection = sortColumns[i];
foreach (var s in sort)
dataGrid.Items.SortDescriptions.Add(s);
}
if (selectedId != null && source != null)
dataGrid.SelectedItem = source.Cast<object>().FirstOrDefault(i => selectedId.Equals(getId(i)));
if (dataGrid.SelectedItem != null && column != null)
dataGrid.CurrentCell = new(dataGrid.SelectedItem, column);
if (source != null && dataGrid.SelectedItem == null) {
if ((def == RenewSourceDefault.IfOnly && source.Cast<object>().Count() == 1) || def == RenewSourceDefault.First) {
dataGrid.SelectedItem = source.Cast<object>().First();
}
}
if (handler != null) dataGrid.SelectionChanged += handler;
}
public static void RenewItemsSource(ListBox listBox, IEnumerable? source, Func<object?, object?> getId, SelectionChangedEventHandler? handler = null, RenewSourceDefault def = RenewSourceDefault.None) {
if (listBox.ItemsSource == source)
return;
var selectedId = getId(listBox.SelectedItem);
if (handler != null) listBox.SelectionChanged -= handler;
listBox.ItemsSource = source;
if (selectedId != null && source != null)
listBox.SelectedItem = source.Cast<object>().FirstOrDefault(i => selectedId.Equals(getId(i)));
if (source != null && listBox.SelectedItem == null) {
if ((def == RenewSourceDefault.IfOnly && source.Cast<object>().Count() == 1) || def == RenewSourceDefault.First) {
listBox.SelectedItem = source.Cast<object>().First();
}
}
if (handler != null) listBox.SelectionChanged += handler;
}
public static object? GetItemFromSource(IEnumerable source, Func<object?, object?> getId, object? id) {
if (source == null)
return null;
var items = source.Cast<object>();
var item = items.Where(i => getId(i)?.Equals(id) ?? false).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<object?, object?> getId) {
return GetItemFromSource(source, getId, getId(item));
}
public static void SelectComboBoxItem(ComboBox cb, Func<object?, object?> getId, object? id) {
cb.SelectedItem = GetItemFromSource(cb.ItemsSource, getId, id);
}
public static void SelectComboBoxItem(ComboBox cb, object? item, Func<object?, object?> getId) {
SelectComboBoxItem(cb, getId, getId(item));
}
public static IEnumerable<object?> GetItemsFromSource(IEnumerable source, Func<object?, object?> getId, IEnumerable<object?> ids) {
if (source == null)
return Array.Empty<object>();
return source.Cast<object>().Where(i => ids.Any(c => c?.Equals(getId(i)) ?? false));
}
public static IEnumerable<object?> GetItemsFromSource(IEnumerable source, IEnumerable<object?>? items, Func<object?, object?> getId) {
if (items == null)
return Array.Empty<object>();
return GetItemsFromSource(source, getId, items.Select(i => getId(i)));
}
public static void SelectCheckComboBoxItems(Xceed.Wpf.Toolkit.CheckComboBox ccb, Func<object?, object?> getId, IEnumerable<object?>? ids) {
ccb.SelectedItems.Clear();
if (ids == null) return;
foreach (var id in ids)
ccb.SelectedItems.Add(GetItemFromSource(ccb.ItemsSource, getId, id));
}
public static void SelectCheckComboBoxItems(Xceed.Wpf.Toolkit.CheckComboBox ccb, IEnumerable<object>? items, Func<object?, object?> getId) {
SelectCheckComboBoxItems(ccb, getId, items?.Select(i => getId(i)));
}
}
}

View File

@ -2,15 +2,11 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using System.Windows.Controls.Primitives;
using System.Text.RegularExpressions;
using System.IO.Ports;
using System.Net.Sockets;
using System.Collections;
namespace Elwig.Helpers {
public static partial class Utils {
@ -65,161 +61,6 @@ namespace Elwig.Helpers {
return client;
}
private static void SetControlBrush(Control input, Brush brush) {
if (input is ComboBox cb) {
var border = GetComboBoxBorder(cb);
if (border != null) border.BorderBrush = brush;
} else if (input is Xceed.Wpf.Toolkit.CheckComboBox ccb) {
var border = GetComboBoxBorder(ccb);
if (border != null) border.BorderBrush = brush;
} else {
input.BorderBrush = brush;
}
}
public static void SetInputChanged(Control input) {
SetControlBrush(input, Brushes.Orange);
}
public static void SetInputInvalid(Control input) {
SetControlBrush(input, Brushes.Red);
}
public static void ClearInputState(Control input) {
if (input is ComboBox cb) {
GetComboBoxBorder(cb)?.ClearValue(Border.BorderBrushProperty);
} else if (input is Xceed.Wpf.Toolkit.CheckComboBox ccb) {
GetComboBoxBorder(ccb)?.ClearValue(Border.BorderBrushProperty);
} else {
input.ClearValue(Control.BorderBrushProperty);
}
}
private static Border? GetComboBoxBorder(ComboBox cb) {
var toggleButton = cb.Template.FindName("toggleButton", cb) as ToggleButton;
return toggleButton?.Template.FindName("templateRoot", toggleButton) as Border;
}
private static Border? GetComboBoxBorder(Xceed.Wpf.Toolkit.CheckComboBox ccb) {
var toggleButton = ccb.Template.FindName("toggleButton", ccb) as ToggleButton;
return toggleButton?.Template.FindName("templateRoot", toggleButton) as Border;
}
public static IEnumerable<T> FindAllChildren<T>(DependencyObject depObj) where T : DependencyObject {
if (depObj == null)
yield return (T)Enumerable.Empty<T>();
foreach (var child in LogicalTreeHelper.GetChildren(depObj)) {
if (child == null) {
continue;
} else if (child is T t) {
yield return t;
} else if (child is DependencyObject childDepOpj) {
foreach (T childOfChild in FindAllChildren<T>(childDepOpj)) {
yield return childOfChild;
}
}
}
}
public static IEnumerable<T> FindAllChildren<T>(DependencyObject depObj, IEnumerable<DependencyObject> exempt) where T : DependencyObject {
return FindAllChildren<T>(depObj).Where(c => !exempt.Contains(c));
}
public static T? FindNextSibling<T>(Control me) where T : DependencyObject {
var found = false;
foreach (var child in LogicalTreeHelper.GetChildren(me.Parent)) {
if (found && child is T c) {
return c;
} else if (child == me) {
found = true;
}
}
return null;
}
public enum RenewSourceDefault {
None,
IfOnly,
First
}
public static void RenewItemsSource(Selector selector, IEnumerable? source, Func<object?, object?> getId, SelectionChangedEventHandler? handler = null, RenewSourceDefault def = RenewSourceDefault.None) {
if (selector.ItemsSource == source)
return;
var selectedId = getId(selector.SelectedItem);
if (handler != null) selector.SelectionChanged -= handler;
selector.ItemsSource = source;
if (selectedId != null && source != null)
selector.SelectedItem = source.Cast<object>().FirstOrDefault(i => selectedId.Equals(getId(i)));
if (source != null && selector.SelectedItem == null) {
if ((def == RenewSourceDefault.IfOnly && source.Cast<object>().Count() == 1) || def == RenewSourceDefault.First) {
selector.SelectedItem = source.Cast<object>().First();
}
}
if (handler != null) selector.SelectionChanged += handler;
}
public static void RenewItemsSource(Xceed.Wpf.Toolkit.Primitives.Selector selector, IEnumerable? source, Func<object?, object?> getId, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventHandler? handler = null, RenewSourceDefault def = RenewSourceDefault.None) {
if (selector.ItemsSource == source)
return;
var selectedIds = selector.SelectedItems.Cast<object>().Select(i => getId(i)).ToList();
if (handler != null) selector.ItemSelectionChanged -= handler;
selector.ItemsSource = source;
if (source != null) {
foreach (var i in source.Cast<object>().Where(i => selectedIds.Contains(getId(i))))
selector.SelectedItems.Add(i);
}
if (source != null && selector.SelectedItem == null) {
if ((def == RenewSourceDefault.IfOnly && source.Cast<object>().Count() == 1) || def == RenewSourceDefault.First) {
selector.SelectedItem = source.Cast<object>().First();
}
}
if (handler != null) selector.ItemSelectionChanged += handler;
}
public static void RenewItemsSource(DataGrid dataGrid, IEnumerable? source, Func<object?, object?> getId, 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);
if (handler != null) dataGrid.SelectionChanged -= handler;
dataGrid.ItemsSource = source;
if (keepSort) {
for (int i = 0; i < dataGrid.Columns.Count; i++)
dataGrid.Columns[i].SortDirection = sortColumns[i];
foreach (var s in sort)
dataGrid.Items.SortDescriptions.Add(s);
}
if (selectedId != null && source != null)
dataGrid.SelectedItem = source.Cast<object>().FirstOrDefault(i => selectedId.Equals(getId(i)));
if (dataGrid.SelectedItem != null && column != null)
dataGrid.CurrentCell = new(dataGrid.SelectedItem, column);
if (source != null && dataGrid.SelectedItem == null) {
if ((def == RenewSourceDefault.IfOnly && source.Cast<object>().Count() == 1) || def == RenewSourceDefault.First) {
dataGrid.SelectedItem = source.Cast<object>().First();
}
}
if (handler != null) dataGrid.SelectionChanged += handler;
}
public static void RenewItemsSource(ListBox listBox, IEnumerable? source, Func<object?, object?> getId, SelectionChangedEventHandler? handler = null, RenewSourceDefault def = RenewSourceDefault.None) {
if (listBox.ItemsSource == source)
return;
var selectedId = getId(listBox.SelectedItem);
if (handler != null) listBox.SelectionChanged -= handler;
listBox.ItemsSource = source;
if (selectedId != null && source != null)
listBox.SelectedItem = source.Cast<object>().FirstOrDefault(i => selectedId.Equals(getId(i)));
if (source != null && listBox.SelectedItem == null) {
if ((def == RenewSourceDefault.IfOnly && source.Cast<object>().Count() == 1) || def == RenewSourceDefault.First) {
listBox.SelectedItem = source.Cast<object>().First();
}
}
if (handler != null) listBox.SelectionChanged += handler;
}
public static int Modulo(string a, int b) {
if (a.Length == 0 || !a.All(char.IsAsciiDigit)) {
throw new ArgumentException("First argument has to be a decimal string");
@ -293,50 +134,5 @@ namespace Elwig.Helpers {
}
return i;
}
public static object? GetItemFromSource(IEnumerable source, Func<object?, object?> getId, object? id) {
if (source == null)
return null;
var items = source.Cast<object>();
var item = items.Where(i => getId(i)?.Equals(id) ?? false).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<object?, object?> getId) {
return GetItemFromSource(source, getId, getId(item));
}
public static void SelectComboBoxItem(ComboBox cb, Func<object?, object?> getId, object? id) {
cb.SelectedItem = GetItemFromSource(cb.ItemsSource, getId, id);
}
public static void SelectComboBoxItem(ComboBox cb, object? item, Func<object?, object?> getId) {
SelectComboBoxItem(cb, getId, getId(item));
}
public static IEnumerable<object?> GetItemsFromSource(IEnumerable source, Func<object?, object?> getId, IEnumerable<object?> ids) {
if (source == null)
return Array.Empty<object>();
return source.Cast<object>().Where(i => ids.Any(c => c?.Equals(getId(i)) ?? false));
}
public static IEnumerable<object?> GetItemsFromSource(IEnumerable source, IEnumerable<object?>? items, Func<object?, object?> getId) {
if (items == null)
return Array.Empty<object>();
return GetItemsFromSource(source, getId, items.Select(i => getId(i)));
}
public static void SelectCheckComboBoxItems(Xceed.Wpf.Toolkit.CheckComboBox ccb, Func<object?, object?> getId, IEnumerable<object?>? ids) {
ccb.SelectedItems.Clear();
if (ids == null) return;
foreach (var id in ids)
ccb.SelectedItems.Add(GetItemFromSource(ccb.ItemsSource, getId, id));
}
public static void SelectCheckComboBoxItems(Xceed.Wpf.Toolkit.CheckComboBox ccb, IEnumerable<object>? items, Func<object?, object?> getId) {
SelectCheckComboBoxItems(ccb, getId, items?.Select(i => getId(i)));
}
}
}

View File

@ -63,13 +63,13 @@ namespace Elwig.Windows {
}
private void OnLoaded(object sender, RoutedEventArgs evt) {
TextBoxInputs = Utils.FindAllChildren<TextBox>(this, ExemptInputs).ToArray();
ComboBoxInputs = Utils.FindAllChildren<ComboBox>(this, ExemptInputs).ToArray();
CheckBoxInputs = Utils.FindAllChildren<CheckBox>(this, ExemptInputs).ToArray();
CheckComboBoxInputs = Utils.FindAllChildren<CheckComboBox>(this, ExemptInputs).ToArray();
RadioButtonInputs = Utils.FindAllChildren<RadioButton>(this, ExemptInputs).ToArray();
TextBoxInputs = ControlUtils.FindAllChildren<TextBox>(this, ExemptInputs).ToArray();
ComboBoxInputs = ControlUtils.FindAllChildren<ComboBox>(this, ExemptInputs).ToArray();
CheckBoxInputs = ControlUtils.FindAllChildren<CheckBox>(this, ExemptInputs).ToArray();
CheckComboBoxInputs = ControlUtils.FindAllChildren<CheckComboBox>(this, ExemptInputs).ToArray();
RadioButtonInputs = ControlUtils.FindAllChildren<RadioButton>(this, ExemptInputs).ToArray();
PlzInputs = TextBoxInputs.Where(tb => "PLZ".Equals(tb.Tag)).ToArray();
PlzOrtInputs = PlzInputs.Select(tb => Utils.FindNextSibling<ComboBox>(tb) ?? throw new MissingMemberException()).ToArray();
PlzOrtInputs = PlzInputs.Select(tb => ControlUtils.FindNextSibling<ComboBox>(tb) ?? throw new MissingMemberException()).ToArray();
foreach (var tb in TextBoxInputs)
Valid[tb] = true;
foreach (var cb in ComboBoxInputs)
@ -104,24 +104,24 @@ namespace Elwig.Windows {
protected void ClearInputStates() {
foreach (var tb in TextBoxInputs)
Utils.ClearInputState(tb);
ControlUtils.ClearInputState(tb);
foreach (var cb in ComboBoxInputs)
Utils.ClearInputState(cb);
ControlUtils.ClearInputState(cb);
foreach (var ccb in CheckComboBoxInputs)
Utils.ClearInputState(ccb);
ControlUtils.ClearInputState(ccb);
foreach (var cb in CheckBoxInputs)
Utils.ClearInputState(cb);
ControlUtils.ClearInputState(cb);
foreach (var rb in RadioButtonInputs)
Utils.ClearInputState(rb);
ControlUtils.ClearInputState(rb);
}
protected void ValidateRequiredInputs() {
foreach (var input in RequiredInputs) {
if (input is TextBox tb && tb.Text.Length == 0) {
Utils.SetInputInvalid(input);
ControlUtils.SetInputInvalid(input);
Valid[input] = false;
} else if (input is ComboBox cb && cb.SelectedItem == null && cb.ItemsSource != null) {
Utils.SetInputInvalid(input);
ControlUtils.SetInputInvalid(input);
}
}
}
@ -230,7 +230,7 @@ namespace Elwig.Windows {
var plzInputValid = GetInputValid(plzInput);
var item = ortInput.SelectedItem;
var list = plzInputValid && plzInput.Text.Length == 4 ? Context.Postleitzahlen.Find(int.Parse(plzInput.Text))?.Orte.ToList() : null;
Utils.RenewItemsSource(ortInput, list, i => (i as AT_PlzDest)?.Id);
ControlUtils.RenewItemsSource(ortInput, list, i => (i as AT_PlzDest)?.Id);
if (list != null && ortInput.SelectedItem == null && list.Count == 1)
ortInput.SelectedItem = list[0];
UpdateComboBox(ortInput);
@ -264,12 +264,12 @@ namespace Elwig.Windows {
ValidateInput(input, res.IsValid);
if (res.IsValid) {
if (InputHasChanged(input)) {
Utils.SetInputChanged(input);
ControlUtils.SetInputChanged(input);
} else {
Utils.ClearInputState(input);
ControlUtils.ClearInputState(input);
}
} else {
Utils.SetInputInvalid(input);
ControlUtils.SetInputInvalid(input);
}
UpdateButtons();
return res.IsValid;
@ -292,11 +292,11 @@ namespace Elwig.Windows {
protected void CheckBox_Changed(object sender, RoutedEventArgs evt) {
var input = (CheckBox)sender;
if (SenderIsRequired(input) && input.IsChecked != true) {
Utils.SetInputInvalid(input);
ControlUtils.SetInputInvalid(input);
} else if (InputHasChanged(input)) {
Utils.SetInputChanged(input);
ControlUtils.SetInputChanged(input);
} else {
Utils.ClearInputState(input);
ControlUtils.ClearInputState(input);
}
UpdateButtons();
}
@ -304,11 +304,11 @@ namespace Elwig.Windows {
protected void RadioButton_Changed(object sender, RoutedEventArgs evt) {
var input = (RadioButton)sender;
if (SenderIsRequired(input) && input.IsChecked != true) {
Utils.SetInputInvalid(input);
ControlUtils.SetInputInvalid(input);
} else if (InputHasChanged(input)) {
Utils.SetInputChanged(input);
ControlUtils.SetInputChanged(input);
} else {
Utils.ClearInputState(input);
ControlUtils.ClearInputState(input);
}
UpdateButtons();
}
@ -317,13 +317,13 @@ namespace Elwig.Windows {
var input = (TextBox)sender;
if (SenderIsRequired(input) && input.Text.Length == 0) {
ValidateInput(input, false);
Utils.SetInputInvalid(input);
ControlUtils.SetInputInvalid(input);
} else {
ValidateInput(input, true);
if (InputHasChanged(input)) {
Utils.SetInputChanged(input);
ControlUtils.SetInputChanged(input);
} else {
Utils.ClearInputState(input);
ControlUtils.ClearInputState(input);
}
}
UpdateButtons();
@ -339,13 +339,13 @@ namespace Elwig.Windows {
if (valid) {
ValidateInput(input, true);
if (InputHasChanged(input)) {
Utils.SetInputChanged(input);
ControlUtils.SetInputChanged(input);
} else {
Utils.ClearInputState(input);
ControlUtils.ClearInputState(input);
}
} else {
ValidateInput(input, false);
Utils.SetInputInvalid(input);
ControlUtils.SetInputInvalid(input);
}
UpdateButtons();
}

View File

@ -41,7 +41,7 @@ namespace Elwig.Windows {
private async Task RefreshAreaCommitmentListQuery() {
List<AreaCom> areaComs = await Context.AreaCommitments.Where(a => a.MgNr == Member.MgNr).OrderBy(a => a.FbNr).ToListAsync();
Utils.RenewItemsSource(AreaCommitmentList, areaComs, i => (i as AreaCom)?.FbNr);
ControlUtils.RenewItemsSource(AreaCommitmentList, areaComs, i => (i as AreaCom)?.FbNr);
if (areaComs.Count == 1)
AreaCommitmentList.SelectedIndex = 0;
@ -102,10 +102,10 @@ namespace Elwig.Windows {
protected override async Task RenewContext() {
await base.RenewContext();
Utils.RenewItemsSource(KgInput, await Context.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToListAsync(), i => (i as AT_Kg)?.KgNr);
Utils.RenewItemsSource(WineVarietyInput, await Context.WineVarieties.OrderBy(v => v.Name).ToListAsync(), i => (i as WineVar)?.SortId);
Utils.RenewItemsSource(AttributesInput, await Context.WineAttributes.OrderBy(a => a.Name).ToListAsync(), i => (i as WineAttr)?.AttrId);
Utils.RenewItemsSource(WineCultivationInput, await Context.WineCultivations.OrderBy(c => c.Name).ToListAsync(), i => (i as WineCult)?.CultId);
ControlUtils.RenewItemsSource(KgInput, await Context.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToListAsync(), i => (i as AT_Kg)?.KgNr);
ControlUtils.RenewItemsSource(WineVarietyInput, await Context.WineVarieties.OrderBy(v => v.Name).ToListAsync(), i => (i as WineVar)?.SortId);
ControlUtils.RenewItemsSource(AttributesInput, await Context.WineAttributes.OrderBy(a => a.Name).ToListAsync(), i => (i as WineAttr)?.AttrId);
ControlUtils.RenewItemsSource(WineCultivationInput, await Context.WineCultivations.OrderBy(c => c.Name).ToListAsync(), i => (i as WineCult)?.CultId);
await RefreshAreaCommitmentList();
}
@ -301,7 +301,7 @@ namespace Elwig.Windows {
if (curr_kg != null) {
var rdList = await Context.WbRde.Where(r => r.KgNr == curr_kg.KgNr).OrderBy(r => r.Name).Cast<object>().ToListAsync();
rdList.Insert(0, new NullItem());
Utils.RenewItemsSource(RdInput, rdList, i => (i as WbRd)?.RdNr);
ControlUtils.RenewItemsSource(RdInput, rdList, i => (i as WbRd)?.RdNr);
}
ComboBox_SelectionChanged(sender, evt);
}

View File

@ -121,7 +121,7 @@ namespace Elwig.Windows {
.ToList();
}
Utils.RenewItemsSource(DeliveryList, deliveries, d => ((d as Delivery)?.Year, (d as Delivery)?.DId), DeliveryList_SelectionChanged, Utils.RenewSourceDefault.IfOnly, !updateSort);
ControlUtils.RenewItemsSource(DeliveryList, deliveries, d => ((d as Delivery)?.Year, (d as Delivery)?.DId), DeliveryList_SelectionChanged, ControlUtils.RenewSourceDefault.IfOnly, !updateSort);
}
protected override async Task RenewContext() {
@ -139,16 +139,16 @@ namespace Elwig.Windows {
await RefreshDeliveryList();
var d = DeliveryList.SelectedItem as Delivery;
var y = d?.Year ?? Utils.CurrentLastSeason;
Utils.RenewItemsSource(MemberInput, await Context.Members.OrderBy(m => m.FamilyName).ThenBy(m => m.GivenName).ToListAsync(), i => (i as Member)?.MgNr);
Utils.RenewItemsSource(BranchInput, await Context.Branches.OrderBy(b => b.Name).ToListAsync(), i => (i as Branch)?.ZwstId);
Utils.RenewItemsSource(WineVarietyInput, await Context.WineVarieties.OrderBy(v => v.Name).ToListAsync(), i => (i as WineVar)?.SortId);
Utils.RenewItemsSource(AttributesInput, await Context.WineAttributes.OrderBy(a => a.Name).ToListAsync(), i => (i as WineAttr)?.AttrId);
Utils.RenewItemsSource(WineQualityLevelInput, await Context.WineQualityLevels.ToListAsync(), i => (i as WineQualLevel)?.QualId);
Utils.RenewItemsSource(ModifiersInput, await Context.Modifiers.Where(m => m.Year == y).OrderBy(m => m.Name).ToListAsync(), i => (i as Modifier)?.ModId);
Utils.RenewItemsSource(WineOriginInput, (await Context.WineOrigins.ToListAsync()).OrderByDescending(o => o.SortKey).ThenBy(o => o.HkId), i => (i as WineOrigin)?.HkId);
ControlUtils.RenewItemsSource(MemberInput, await Context.Members.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(AttributesInput, await Context.WineAttributes.OrderBy(a => a.Name).ToListAsync(), i => (i as WineAttr)?.AttrId);
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.Name).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);
var kgList = await Context.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).Cast<object>().ToListAsync();
kgList.Insert(0, new NullItem());
Utils.RenewItemsSource(WineKgInput, kgList, i => (i as AT_Kg)?.KgNr);
ControlUtils.RenewItemsSource(WineKgInput, kgList, i => (i as AT_Kg)?.KgNr);
UpdateRdInput();
if (IsCreating) await UpdateLsNr();
@ -163,10 +163,10 @@ namespace Elwig.Windows {
private void RefreshDeliveryParts() {
if (DeliveryList.SelectedItem is Delivery d) {
Utils.RenewItemsSource(ModifiersInput, Context.Modifiers.Where(m => m.Year == d.Year).OrderBy(m => m.Name).ToList(), i => (i as Modifier)?.ModId);
Utils.RenewItemsSource(DeliveryPartList, d.Parts.OrderBy(p => p.DPNr).ToList(), i => ((i as DeliveryPart)?.Year, (i as DeliveryPart)?.DId, (i as DeliveryPart)?.DPNr), DeliveryPartList_SelectionChanged, Utils.RenewSourceDefault.First);
ControlUtils.RenewItemsSource(ModifiersInput, Context.Modifiers.Where(m => m.Year == d.Year).OrderBy(m => m.Name).ToList(), i => (i as Modifier)?.ModId);
ControlUtils.RenewItemsSource(DeliveryPartList, d.Parts.OrderBy(p => p.DPNr).ToList(), i => ((i as DeliveryPart)?.Year, (i as DeliveryPart)?.DId, (i as DeliveryPart)?.DPNr), DeliveryPartList_SelectionChanged, ControlUtils.RenewSourceDefault.First);
} else {
Utils.RenewItemsSource(ModifiersInput, Context.Modifiers.Where(m => m.Year == Utils.CurrentLastSeason).OrderBy(m => m.Name).ToList(), i => (i as Modifier)?.ModId);
ControlUtils.RenewItemsSource(ModifiersInput, Context.Modifiers.Where(m => m.Year == Utils.CurrentLastSeason).OrderBy(m => m.Name).ToList(), i => (i as Modifier)?.ModId);
DeliveryPartList.ItemsSource = null;
}
}
@ -187,23 +187,23 @@ namespace Elwig.Windows {
var d = p.Delivery;
MgNrInput.Text = d.MgNr.ToString();
Utils.SelectComboBoxItem(BranchInput, i => (i as Branch)?.ZwstId, d.ZwstId);
ControlUtils.SelectComboBoxItem(BranchInput, i => (i as Branch)?.ZwstId, d.ZwstId);
LsNrInput.Text = d.LsNr;
DateInput.Text = d.Date.ToString("dd.MM.yyyy");
TimeInput.Text = d.Time?.ToString("HH:mm") ?? "";
CommentInput.Text = d.Comment ?? "";
SortIdInput.Text = p?.SortId ?? "";
Utils.SelectCheckComboBoxItems(AttributesInput, p?.Attributes, i => (i as WineAttr)?.AttrId);
ControlUtils.SelectCheckComboBoxItems(AttributesInput, p?.Attributes, i => (i as WineAttr)?.AttrId);
GradationKmwInput.Text = (p != null) ? $"{p.Kmw:N1}" : "";
Utils.SelectComboBoxItem(WineQualityLevelInput, q => (q as WineQualLevel)?.QualId, p?.QualId);
Utils.SelectComboBoxItem(WineKgInput, k => (k as AT_Kg)?.KgNr, p?.KgNr);
Utils.SelectComboBoxItem(WineRdInput, r => (r as WbRd)?.RdNr, p?.RdNr);
Utils.SelectComboBoxItem(WineOriginInput, r => (r as WineOrigin)?.HkId, p?.HkId);
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);
WeightInput.Text = p?.Weight.ToString() ?? "";
ManualWeighingInput.IsChecked = p?.ManualWeighing ?? false;
GerebeltGewogenInput.IsChecked = p?.IsGerebelt ?? false;
Utils.SelectCheckComboBoxItems(ModifiersInput, p?.Modifiers, i => (i as Modifier)?.ModId);
ControlUtils.SelectCheckComboBoxItems(ModifiersInput, p?.Modifiers, i => (i as Modifier)?.ModId);
PartCommentInput.Text = p?.Comment ?? "";
TemperatureInput.Text = p?.Temperature?.ToString() ?? "";
AcidInput.Text = p?.Acid?.ToString() ?? "";
@ -543,7 +543,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<object>().ToList();
list.Insert(0, new NullItem());
Utils.RenewItemsSource(WineRdInput, list, i => ((i as WbRd)?.KgNr, (i as WbRd)?.RdNr));
ControlUtils.RenewItemsSource(WineRdInput, list, i => ((i as WbRd)?.KgNr, (i as WbRd)?.RdNr));
if (WineRdInput.SelectedItem == null) WineRdInput.SelectedIndex = 0;
WineRdInput.IsEnabled = (IsEditing || IsCreating) && list.Count > 1;
} else {

View File

@ -89,7 +89,7 @@ namespace Elwig.Windows {
.ToList();
}
Utils.RenewItemsSource(MemberList, members, i => (i as Member)?.MgNr, MemberList_SelectionChanged, Utils.RenewSourceDefault.IfOnly, !updateSort);
ControlUtils.RenewItemsSource(MemberList, members, i => (i as Member)?.MgNr, MemberList_SelectionChanged, ControlUtils.RenewSourceDefault.IfOnly, !updateSort);
}
private void RefreshInputs(bool validate = false) {
@ -124,8 +124,8 @@ namespace Elwig.Windows {
protected override async Task RenewContext() {
await base.RenewContext();
Utils.RenewItemsSource(BranchInput, await Context.Branches.OrderBy(b => b.Name).ToListAsync(), i => (i as Branch)?.ZwstId);
Utils.RenewItemsSource(DefaultKgInput, await Context.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToListAsync(), i => (i as AT_Kg)?.KgNr);
ControlUtils.RenewItemsSource(BranchInput, await Context.Branches.OrderBy(b => b.Name).ToListAsync(), i => (i as Branch)?.ZwstId);
ControlUtils.RenewItemsSource(DefaultKgInput, await Context.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToListAsync(), i => (i as AT_Kg)?.KgNr);
await RefreshMemberList();
}