| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- //----------------------------------------------------------------
- //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<Window>(this);
- if (parent != null) parent.DragMove();
- }
- private void MuchinfoWindowControl_CloseButtonClicked(object sender, RoutedEventArgs e)
- {
- var parent = FindVisualParent<Window>(this);
- if (parent != null) parent.Close();
- }
- /// <summary>
- /// 从父容器中查找指定类型的控件
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="parent"></param>
- /// <returns></returns>
- private T FindVisualChild<T>(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<T>(childObject);
- if (childItem != null) return childItem;
- }
- return child;
- }
- return null;
- }
- /// <summary>
- /// 查找指定类型的父容器
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="child">The child.</param>
- /// <returns>``0.</returns>
- private T FindVisualParent<T>(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<T>(parentObject);
- }
- }
- }
|