// Copyright (c) 2017 schick Informatik // Description: Basisklasse für Bearbeitungsfensterle // Position merken usw. using System; using System.Windows; using System.Windows.Controls; using System.ComponentModel; using System.Text.RegularExpressions; using Xceed.Wpf.Toolkit; using bsmd.database; using ENI2.Util; namespace ENI2.Controls { /// /// Basisklasse aller Bearbeitungsdialoge. OK/Cancel Buttons und Window Placement /// [TemplatePart(Name = "buttonOK", Type = typeof(Button))] [TemplatePart(Name = "buttonCancel", Type = typeof(Button))] [TemplatePart(Name = "buttonAdd", Type = typeof(Button))] public class EditWindowBase : Window { public event Action OKClicked; public event Action CancelClicked; public event Action AddClicked; private static readonly Regex rdt = new Regex(@"^(\d{12})$"); private static readonly Regex rd = new Regex(@"^(\d{8})$"); protected bool shouldCancel; static EditWindowBase() { DefaultStyleKeyProperty.OverrideMetadata(typeof(EditWindowBase), new FrameworkPropertyMetadata(typeof(EditWindowBase))); } public EditWindowBase() { Loaded += (_, __) => { var okButton = (Button)Template.FindName("buttonOK", this); var cancelButton = (Button)Template.FindName("buttonCancel", this); var addButton = (Button)Template.FindName("buttonAdd", this); okButton.Click += (s, e) => { if (this.IsModal()) DialogResult = true; OKClicked?.Invoke(); this.Close(); }; cancelButton.Click += (s, e) => { if (this.IsModal()) DialogResult = false; CancelClicked?.Invoke(); this.Close(); }; addButton.Click += (s, e) => AddClicked?.Invoke(); this.Closing += Window_Closing; }; } public bool AddVisible { get { var addButton = (Button)Template.FindName("buttonAdd", this); return addButton.Visibility == Visibility.Visible; } set { var addButton = (Button)Template.FindName("buttonAdd", this); addButton.Visibility = value ? Visibility.Visible : Visibility.Hidden; var okButton = (Button)Template.FindName("buttonOK", this); if (okButton.Visibility == Visibility.Hidden) okButton.Width = 1; // we are in a DockPanel, try to collapse okButton to place addButton more to the right } } public bool OkVisible { get { var okButton = (Button)Template.FindName("buttonOK", this); return okButton.Visibility == Visibility.Visible; } set { var okButton = (Button)Template.FindName("buttonOK", this); okButton.Visibility = value ? Visibility.Visible : Visibility.Hidden; } } private void Window_Closing(object sender, CancelEventArgs e) { if (this.shouldCancel) e.Cancel = true; } protected void EnableOK(bool enabled) { var okButton = (Button)Template.FindName("buttonOK", this); okButton.IsEnabled = enabled; } protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); // this.SetPlacement(..) } protected virtual void OnOkClicked() { this.DialogResult = true; this.shouldCancel = false; OKClicked?.Invoke(); } #region "BHV Spezial" Datetime Parsing.. protected void DateTimePicker_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e) { if ((sender is DateTimePicker thePicker) && !thePicker.Text.IsNullOrEmpty() && rdt.IsMatch(thePicker.Text)) { try { string timevalText = rdt.Match(thePicker.Text).Captures[0].Value; int day = Int32.Parse(timevalText.Substring(0, 2)); int month = Int32.Parse(timevalText.Substring(2, 2)); int year = Int32.Parse(timevalText.Substring(4, 4)); int hour = Int32.Parse(timevalText.Substring(8, 2)); int minute = Int32.Parse(timevalText.Substring(10, 2)); thePicker.Value = new DateTime(year, month, day, hour, minute, 0); } catch (FormatException) { } catch (ArgumentOutOfRangeException) { thePicker.Value = null; } } } protected void DateTimePicker_PreviewKeyUpDate(object sender, System.Windows.Input.KeyEventArgs e) { if ((sender is DatePicker thePicker) && rd.IsMatch(thePicker.Text)) { try { string timevalText = rd.Match(thePicker.Text).Captures[0].Value; int day = Int32.Parse(timevalText.Substring(0, 2)); int month = Int32.Parse(timevalText.Substring(2, 2)); int year = Int32.Parse(timevalText.Substring(4, 4)); thePicker.SelectedDate = new DateTime(year, month, day, 0, 0, 0); } catch (FormatException) { } catch (ArgumentOutOfRangeException) { thePicker.SelectedDate = null; } } } #endregion #region combobox content filtering protected void ComboBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) { ComboBox cmb = sender as ComboBox; GlobalStructures.FilterCombobox(cmb, e.Key); } #endregion } }