git_bsmd/ENI2/CustomCommands.cs

45 lines
1.4 KiB
C#

// Copyright (c) 2017 schick Informatik
// Description: Eigene routed commands. Damit können "globale" Commands definiert werden, die überall in XAML
// an Objekte attached werden können. Erste Verwendung war das Kontextmenü "löschen" für DateTimePicker, um den
// ausgewählten Wert zu leeren.
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
namespace ENI2
{
public static class CustomCommands
{
public static readonly RoutedUICommand Clear = new RoutedUICommand(
Properties.Resources.textClear,
"Clear",
typeof(CustomCommands),
new InputGestureCollection()
{
new KeyGesture(Key.Delete)
}
);
public static T FindParent<T>(DependencyObject child) where T : DependencyObject
{
if (child == null) return null;
//get parent item
DependencyObject parentObject = VisualTreeHelper.GetParent(child);
//we've reached the end of the tree
if (parentObject == null) return null;
//check if the parent matches the type we're looking for
if (parentObject is T parent)
return parent;
else
return FindParent<T>(parentObject);
}
}
}