//---------------------------------------------------------------- //Module Name: MuchinfoWindowControl //Purpose: //CopyRight: Muchinfo //History: //---------------------------------------------------------------- //DateTime Author Description //---------------------------------------------------------------- //2014-04-08 deng.yinping Create //---------------------------------------------------------------- using System.Windows; using System.Windows.Input; using System.Windows.Media; using Xceed.Wpf.Toolkit.Primitives; namespace Muchinfo.WPF.Controls.Windows { public class MuchinfoWindowControl : WindowControl { public MuchinfoWindowControl() { this.CloseButtonClicked += MuchinfoWindowControl_CloseButtonClicked; this.HeaderMouseLeftButtonClicked += MuchinfoWindowControl_HeaderMouseLeftButtonClicked; } private void MuchinfoWindowControl_HeaderMouseLeftButtonClicked(object sender, MouseButtonEventArgs e) { var parent = FindVisualParent(this); if (parent != null) parent.DragMove(); } private void MuchinfoWindowControl_CloseButtonClicked(object sender, RoutedEventArgs e) { var parent = FindVisualParent(this); if (parent != null) parent.Close(); } /// /// 从父容器中查找指定类型的控件 /// /// /// /// private T FindVisualChild(DependencyObject parent) where T : DependencyObject { if (parent == null) return null; for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { var childObject = VisualTreeHelper.GetChild(parent, i); if (childObject == null) return null; var child = childObject as T; if (child == null) { var childItem = FindVisualChild(childObject); if (childItem != null) return childItem; } return child; } return null; } /// /// 查找指定类型的父容器 /// /// /// The child. /// ``0. private T FindVisualParent(DependencyObject child) where T : DependencyObject { if (child == null) return null; var parentObject = VisualTreeHelper.GetParent(child); if (parentObject == null) return null; var parent = parentObject as T; return parent ?? FindVisualParent(parentObject); } } }