From 568eedcdf3aeffe9ed83abcb6f3f40bee280a5d7 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Mon, 13 Mar 2023 18:13:41 +0100 Subject: [PATCH] Change ComboBox border color --- WGneu/Helpers/Utils.cs | 30 +++++++++++++++++++++++--- WGneu/Windows/MemberListWindow.xaml.cs | 17 ++++++++++++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/WGneu/Helpers/Utils.cs b/WGneu/Helpers/Utils.cs index c83b358..ef2d785 100644 --- a/WGneu/Helpers/Utils.cs +++ b/WGneu/Helpers/Utils.cs @@ -6,19 +6,43 @@ using System.Windows.Media; using System.Windows; using System.Windows.Controls; using System.Diagnostics; +using System.Windows.Controls.Primitives; namespace WGneu.Helpers { public static class Utils { public static void SetInputChanged(Control input) { - input.BorderBrush = Brushes.Orange; + var brush = Brushes.Orange; + if (input is ComboBox cb) { + var border = GetComboBoxBorder(cb); + if (border != null) + border.BorderBrush = brush; + } else { + input.BorderBrush = brush; + } } public static void SetInputInvalid(Control input) { - input.BorderBrush = Brushes.Red; + var brush = Brushes.Red; + if (input is ComboBox cb) { + var border = GetComboBoxBorder(cb); + if (border != null) + border.BorderBrush = brush; + } else { + input.BorderBrush = brush; + } } public static void ClearInputState(Control input) { - input.ClearValue(Control.BorderBrushProperty); + if (input is ComboBox cb) { + GetComboBoxBorder(cb)?.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; } public static IEnumerable FindVisualChilds(DependencyObject depObj) where T : DependencyObject { diff --git a/WGneu/Windows/MemberListWindow.xaml.cs b/WGneu/Windows/MemberListWindow.xaml.cs index 3ec33d1..d761a1b 100644 --- a/WGneu/Windows/MemberListWindow.xaml.cs +++ b/WGneu/Windows/MemberListWindow.xaml.cs @@ -114,6 +114,8 @@ namespace WGneu.Windows { EntryDateInput.Text = DateTime.Now.ToString("dd.MM.yyyy"); if (Context.Branches.Count() == 1) BranchInput.SelectedItem = Context.Branches.First(); + ActiveInput.IsChecked = true; + ContactPostInput.IsChecked = true; FillOriginalValues(); } @@ -420,8 +422,11 @@ namespace WGneu.Windows { tb.Text = " "; tb.Text = ""; } - foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) { cb.SelectedItem = null; + if (cb.ItemsSource != null) + Utils.SetInputInvalid(cb); + } foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) cb.IsChecked = false; foreach (var rb in Utils.FindVisualChilds(this, ExemptInputs)) @@ -472,6 +477,11 @@ namespace WGneu.Windows { ortInput.ItemsSource = null; } ortInput.SelectedItem = null; + if (ortInput.ItemsSource != null) { + Utils.SetInputInvalid(ortInput); + } else { + Utils.ClearInputState(ortInput); + } Valid[plzInput] = optional || (ortInput.ItemsSource != null); UpdateButtons(); } @@ -545,8 +555,9 @@ namespace WGneu.Windows { private void ComboBox_SelectionChanged(object sender, RoutedEventArgs evt) { var input = (ComboBox)sender; - if (InputHasChanged(input)) { - // TODO not working + if (input.ItemsSource != null && input.SelectedItem == null) { + Utils.SetInputInvalid(input); + } else if (InputHasChanged(input)) { Utils.SetInputChanged(input); } else { Utils.ClearInputState(input);