// Copyright (c) 2017 schick Informatik // Description: Diese Klasse steuert zentral hervorgehobene Controls. Diese können auf unterschiedliche(!) Art // hervorgehoben sein: NSW Error / NSW Violation / Rule Fehler / Reminder // using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using bsmd.database; using System.Windows.Media; namespace ENI2.Util { public class HighlightService { private static ControlTemplate groupBoxDefaultTemplate; private static Brush dtPickerDefaultColor; private static Brush textBoxDefaultColor; private static Brush intUpDownDefaultColor; private static Brush doubleUpDownDefaultColor; private static Brush checkBoxDefaultColor; #region Construction public HighlightService() { } #endregion #region enums public enum HighlightStyle { NONE, VALIDATION, UPDATE, REMINDER, ERROR, VIOLATION } #endregion #region events public delegate void HighlightChangeNotificationHandler(Message relevantMessage, DatabaseEntity subElement, string identifier); //public event HighlightChangeNotificationHandler PropertyHighlightingChanged; #endregion #region public methods public ControlTemplate GetStyle(string propertyName, object control) { ControlTemplate resultTemplate = null; return resultTemplate; } public bool MessageHasViolations(Message theMessage) { return !theMessage.ViolationList.IsNullOrEmpty(); } public bool MessageHasErrors(Message theMessage) { return !theMessage.ErrorList.IsNullOrEmpty(); } public void HighlightError(MessageError messageError, DependencyObject rootDependency) { IList dList = ValidationContext.GetDependencyObjectsWithBindingToProperty(rootDependency, messageError.PropertyName); if (dList.Count > 0) { HighlightControl(dList[0], HighlightStyle.ERROR, messageError.MessageHeader); } } public void HighlightViolation(MessageViolation messageViolation, DependencyObject rootDependency) { IList dList = ValidationContext.GetDependencyObjectsWithBindingToProperty(rootDependency, messageViolation.PropertyName); if (dList.Count > 0) { HighlightControl(dList[0], HighlightStyle.ERROR, messageViolation.MessageHeader); } } #endregion #region public static methods public static void HighlightControl(object control, HighlightStyle highlightStyle, Message theMessage) { if(control is GroupBox) { GroupBox gp = (GroupBox)control; if (groupBoxDefaultTemplate == null) // save for restore groupBoxDefaultTemplate = gp.Template; switch (highlightStyle) { case HighlightStyle.ERROR: { StringBuilder sb = new StringBuilder(); foreach (MessageError me in theMessage.ErrorList) sb.AppendFormat("{0}: {1}\n", me.ErrorCode, me.ErrorText); gp.ToolTip = sb.ToString(); gp.Template = Application.Current.MainWindow.TryFindResource("groupBoxTemplateError") as ControlTemplate; } break; case HighlightStyle.VIOLATION: { StringBuilder sb = new StringBuilder(); foreach (MessageViolation mv in theMessage.ViolationList) sb.AppendFormat("{0}: {1}\n", mv.ViolationCode, mv.ViolationText); gp.ToolTip = sb.ToString(); gp.Template = Application.Current.MainWindow.FindResource("groupBoxTemplateViolation") as ControlTemplate; } break; default: gp.Template = groupBoxDefaultTemplate; gp.ToolTip = null; break; } } if(control is Xceed.Wpf.Toolkit.DateTimePicker) { Xceed.Wpf.Toolkit.DateTimePicker dtPicker = (Xceed.Wpf.Toolkit.DateTimePicker)control; if (dtPickerDefaultColor == null) dtPickerDefaultColor = dtPicker.Background; switch (highlightStyle) { case HighlightStyle.ERROR: dtPicker.Background = new SolidColorBrush(Color.FromArgb(100, 255, 0, 0)); break; } } if(control is TextBox) { TextBox textBox = (TextBox)control; if (textBoxDefaultColor == null) textBoxDefaultColor = textBox.Background; switch(highlightStyle) { case HighlightStyle.ERROR: textBox.Background = new SolidColorBrush(Color.FromArgb(150, 255, 0, 0)); break; } } if(control is Xceed.Wpf.Toolkit.IntegerUpDown) { Xceed.Wpf.Toolkit.IntegerUpDown integerUpDown = (Xceed.Wpf.Toolkit.IntegerUpDown)control; if (intUpDownDefaultColor == null) intUpDownDefaultColor = integerUpDown.Background; switch (highlightStyle) { case HighlightStyle.ERROR: integerUpDown.Background = new SolidColorBrush(Color.FromArgb(150, 255, 0, 0)); break; } } if(control is Xceed.Wpf.Toolkit.DoubleUpDown) { Xceed.Wpf.Toolkit.DoubleUpDown doubleUpDown = (Xceed.Wpf.Toolkit.DoubleUpDown)control; if (doubleUpDownDefaultColor == null) doubleUpDownDefaultColor = doubleUpDown.Background; switch (highlightStyle) { case HighlightStyle.ERROR: doubleUpDown.Background = new SolidColorBrush(Color.FromArgb(150, 255, 0, 0)); break; } } if(control is CheckBox) { CheckBox cb = (CheckBox)control; if (checkBoxDefaultColor == null) checkBoxDefaultColor = cb.Background; switch(highlightStyle) { case HighlightStyle.ERROR: cb.Background = new SolidColorBrush(Color.FromArgb(150, 255, 0, 0)); break; } } } #endregion #region internal / private methods protected void OnPropertyHighlightingChanged() { } #endregion } }