From c4d68d11bce4fb5468584aca481da786c71ea0d3 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Sun, 28 Apr 2024 20:07:09 +0200 Subject: [PATCH] Controls: Rewrite UnitTextBox as extension of TextBox instead of UserControl --- Elwig/App.xaml | 2 +- Elwig/Controls/UnitTextBox.cs | 17 ++++ Elwig/Controls/UnitTextBox.xaml | 41 +++++++--- Elwig/Controls/UnitTextBox.xaml.cs | 37 --------- ...Converter.cs => WidthToMarginConverter.cs} | 6 +- Elwig/Themes/Generic.xaml | 5 ++ Elwig/Windows/AdministrationWindow.cs | 16 +--- Elwig/Windows/AreaComAdminWindow.xaml.cs | 2 +- Elwig/Windows/BaseDataWindow.xaml | 8 ++ Elwig/Windows/BaseDataWindow.xaml.cs | 78 +++++++++---------- Elwig/Windows/ChartWindow.xaml | 9 +++ Elwig/Windows/ChartWindow.xaml.cs | 8 +- Elwig/Windows/DeliveryAdminWindow.xaml | 8 ++ Elwig/Windows/DeliveryAdminWindow.xaml.cs | 16 ++-- Elwig/Windows/PaymentVariantsWindow.xaml | 8 ++ Elwig/Windows/PaymentVariantsWindow.xaml.cs | 12 +-- 16 files changed, 150 insertions(+), 123 deletions(-) create mode 100644 Elwig/Controls/UnitTextBox.cs delete mode 100644 Elwig/Controls/UnitTextBox.xaml.cs rename Elwig/Controls/{WidthToPaddingConverter.cs => WidthToMarginConverter.cs} (67%) create mode 100644 Elwig/Themes/Generic.xaml diff --git a/Elwig/App.xaml b/Elwig/App.xaml index 6b2a86f..01309ea 100644 --- a/Elwig/App.xaml +++ b/Elwig/App.xaml @@ -7,7 +7,7 @@ Exit="Application_Exit"> - + diff --git a/Elwig/Controls/UnitTextBox.cs b/Elwig/Controls/UnitTextBox.cs new file mode 100644 index 0000000..be87806 --- /dev/null +++ b/Elwig/Controls/UnitTextBox.cs @@ -0,0 +1,17 @@ +using System.Windows; +using System.Windows.Controls; + +namespace Elwig.Controls { + public class UnitTextBox : TextBox { + + public static readonly DependencyProperty UnitProperty = DependencyProperty.Register("Unit", typeof(string), typeof(UnitTextBox), new FrameworkPropertyMetadata("")); + public string Unit { + get => (string)GetValue(UnitProperty); + set => SetValue(UnitProperty, value); + } + + static UnitTextBox() { + DefaultStyleKeyProperty.OverrideMetadata(typeof(UnitTextBox), new FrameworkPropertyMetadata(typeof(UnitTextBox))); + } + } +} diff --git a/Elwig/Controls/UnitTextBox.xaml b/Elwig/Controls/UnitTextBox.xaml index 4fa1e8f..1d09491 100644 --- a/Elwig/Controls/UnitTextBox.xaml +++ b/Elwig/Controls/UnitTextBox.xaml @@ -1,11 +1,30 @@ - - - - - - + + + diff --git a/Elwig/Controls/UnitTextBox.xaml.cs b/Elwig/Controls/UnitTextBox.xaml.cs deleted file mode 100644 index 675fd8d..0000000 --- a/Elwig/Controls/UnitTextBox.xaml.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System.Windows; -using System.Windows.Controls; - -namespace Elwig.Controls { - public partial class UnitTextBox : UserControl { - - public event TextChangedEventHandler? TextChanged; - - public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(UnitTextBox)); - public string Text { - get => (string)GetValue(TextProperty); - set => SetValue(TextProperty, value ?? ""); - } - - public static readonly DependencyProperty UnitProperty = DependencyProperty.Register("Unit", typeof(string), typeof(UnitTextBox)); - public string Unit { - get => (string)GetValue(UnitProperty); - set => SetValue(UnitProperty, value ?? ""); - } - - public UnitTextBox() { - Text = ""; - Unit = ""; - InitializeComponent(); - DataContext = this; - } - - private void TextBox_TextChanged(object sender, TextChangedEventArgs evt) { - Text = TextBox.Text; - TextChanged?.Invoke(sender, evt); - } - - public new void Focus() { - TextBox.Focus(); - } - } -} diff --git a/Elwig/Controls/WidthToPaddingConverter.cs b/Elwig/Controls/WidthToMarginConverter.cs similarity index 67% rename from Elwig/Controls/WidthToPaddingConverter.cs rename to Elwig/Controls/WidthToMarginConverter.cs index b73d888..3013665 100644 --- a/Elwig/Controls/WidthToPaddingConverter.cs +++ b/Elwig/Controls/WidthToMarginConverter.cs @@ -4,13 +4,13 @@ using System.Windows.Data; using System.Globalization; namespace Elwig.Controls { - public class WidthToPaddingConverter : IValueConverter { + public class WidthToMarginConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { - return new Thickness(2, 2, 4 + (double)value, 2); + return new Thickness(0, 0, 2 + (double)value, 0); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { - return ((Thickness)value).Right - 4; + return ((Thickness)value).Right - 2; } } } diff --git a/Elwig/Themes/Generic.xaml b/Elwig/Themes/Generic.xaml new file mode 100644 index 0000000..061707b --- /dev/null +++ b/Elwig/Themes/Generic.xaml @@ -0,0 +1,5 @@ + + + + + diff --git a/Elwig/Windows/AdministrationWindow.cs b/Elwig/Windows/AdministrationWindow.cs index b882323..d57f983 100644 --- a/Elwig/Windows/AdministrationWindow.cs +++ b/Elwig/Windows/AdministrationWindow.cs @@ -133,7 +133,6 @@ namespace Elwig.Windows { } protected void ValidateInput(Control input, bool valid) { - if (input is UnitTextBox utbx) input = utbx.TextBox; Valid[input] = valid; } @@ -234,7 +233,6 @@ namespace Elwig.Windows { } protected void SetOriginalValue(Control input, object? value) { - if (input is UnitTextBox utbx) input = utbx.TextBox; OriginalValues[input] = Utils.GetEntityIdentifier(value); if (InputHasChanged(input)) { ControlUtils.SetInputChanged(input); @@ -244,18 +242,15 @@ namespace Elwig.Windows { } protected void SetOriginalValue(Control input) { - if (input is UnitTextBox utbx) input = utbx.TextBox; SetOriginalValue(input, ControlUtils.GetInputHashCode(input)); } protected void UnsetOriginalValue(Control input) { - if (input is UnitTextBox utbx) input = utbx.TextBox; OriginalValues.Remove(input); ControlUtils.ClearInputState(input); } protected void SetDefaultValue(Control input, object? value) { - if (input is UnitTextBox utbx) input = utbx.TextBox; DefaultValues[input] = Utils.GetEntityIdentifier(value); if (!InputHasChanged(input)) { if (InputIsNotDefault(input)) { @@ -267,12 +262,10 @@ namespace Elwig.Windows { } protected void SetDefaultValue(Control input) { - if (input is UnitTextBox utbx) input = utbx.TextBox; SetDefaultValue(input, ControlUtils.GetInputHashCode(input)); } protected void UnsetDefaultValue(Control input) { - if (input is UnitTextBox utbx) input = utbx.TextBox; DefaultValues.Remove(input); if (!InputHasChanged(input)) { ControlUtils.ClearInputState(input); @@ -296,12 +289,10 @@ namespace Elwig.Windows { protected bool IsValid => Valid.All(kv => kv.Value); protected bool GetInputValid(Control input) { - if (input is UnitTextBox utbx) input = utbx.TextBox; return Valid[input]; } protected bool InputHasChanged(Control input) { - if (input is UnitTextBox utbx) input = utbx.TextBox; if (!OriginalValues.TryGetValue(input, out int? original)) { return false; } else { @@ -311,7 +302,6 @@ namespace Elwig.Windows { } protected bool InputIsNotDefault(Control input) { - if (input is UnitTextBox utbx) input = utbx.TextBox; if (!DefaultValues.TryGetValue(input, out int? defaultValue)) { return false; } else { @@ -436,7 +426,7 @@ namespace Elwig.Windows { protected void TextBox_TextChanged(object sender, RoutedEventArgs? evt) { var input = (Control)sender; - var tb = input as TextBox ?? (input as UnitTextBox)?.TextBox; + var tb = input as TextBox; if (SenderIsRequired(input) && tb?.Text.Length == 0) { ValidateInput(input, false); ControlUtils.SetInputInvalid(input); @@ -482,12 +472,12 @@ namespace Elwig.Windows { protected void IntegerInput_TextChanged(object sender, TextChangedEventArgs evt) { // FIXME - InputTextChanged((sender as UnitTextBox)?.TextBox ?? (TextBox)sender, Validator.CheckInteger); + InputTextChanged((TextBox)sender, Validator.CheckInteger); } protected void DecimalInput_TextChanged(object sender, TextChangedEventArgs evt) { // FIXME - InputTextChanged((sender as UnitTextBox)?.TextBox ?? (TextBox)sender, Validator.CheckDecimal); + InputTextChanged((TextBox)sender, Validator.CheckDecimal); } protected void PartialDateInput_TextChanged(object sender, TextChangedEventArgs evt) { diff --git a/Elwig/Windows/AreaComAdminWindow.xaml.cs b/Elwig/Windows/AreaComAdminWindow.xaml.cs index 1c46713..bc33914 100644 --- a/Elwig/Windows/AreaComAdminWindow.xaml.cs +++ b/Elwig/Windows/AreaComAdminWindow.xaml.cs @@ -29,7 +29,7 @@ namespace Elwig.Windows { ]; RequiredInputs = [ FbNrInput, YearFromInput, KgInput, RdInput, - GstNrInput, AreaInput.TextBox, AreaComTypeInput, WineCultivationInput + GstNrInput, AreaInput, AreaComTypeInput, WineCultivationInput ]; } diff --git a/Elwig/Windows/BaseDataWindow.xaml b/Elwig/Windows/BaseDataWindow.xaml index 9a65199..2efe74c 100644 --- a/Elwig/Windows/BaseDataWindow.xaml +++ b/Elwig/Windows/BaseDataWindow.xaml @@ -24,6 +24,14 @@ + + + +