git_bsmd/ENI-2/ENI2/ENI2/Controls/EditWindowBase.cs

88 lines
3.0 KiB
C#

// 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 ENI2.Util;
namespace ENI2.Controls
{
/// <summary>
/// Basisklasse aller Bearbeitungsdialoge. OK/Cancel Buttons und Window Placement
/// </summary>
[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;
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();
};
}
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; }
}
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 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
}
}