MuchinfoWindowControl.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //----------------------------------------------------------------
  2. //Module Name: MuchinfoWindowControl
  3. //Purpose:
  4. //CopyRight: Muchinfo
  5. //History:
  6. //----------------------------------------------------------------
  7. //DateTime Author Description
  8. //----------------------------------------------------------------
  9. //2014-04-08 deng.yinping Create
  10. //----------------------------------------------------------------
  11. using System.Windows;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using Xceed.Wpf.Toolkit.Primitives;
  15. namespace Muchinfo.WPF.Controls.Windows
  16. {
  17. public class MuchinfoWindowControl : WindowControl
  18. {
  19. public MuchinfoWindowControl()
  20. {
  21. this.CloseButtonClicked += MuchinfoWindowControl_CloseButtonClicked;
  22. this.HeaderMouseLeftButtonClicked += MuchinfoWindowControl_HeaderMouseLeftButtonClicked;
  23. }
  24. private void MuchinfoWindowControl_HeaderMouseLeftButtonClicked(object sender, MouseButtonEventArgs e)
  25. {
  26. var parent = FindVisualParent<Window>(this);
  27. if (parent != null) parent.DragMove();
  28. }
  29. private void MuchinfoWindowControl_CloseButtonClicked(object sender, RoutedEventArgs e)
  30. {
  31. var parent = FindVisualParent<Window>(this);
  32. if (parent != null) parent.Close();
  33. }
  34. /// <summary>
  35. /// 从父容器中查找指定类型的控件
  36. /// </summary>
  37. /// <typeparam name="T"></typeparam>
  38. /// <param name="parent"></param>
  39. /// <returns></returns>
  40. private T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
  41. {
  42. if (parent == null) return null;
  43. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
  44. {
  45. var childObject = VisualTreeHelper.GetChild(parent, i);
  46. if (childObject == null) return null;
  47. var child = childObject as T;
  48. if (child == null)
  49. {
  50. var childItem = FindVisualChild<T>(childObject);
  51. if (childItem != null) return childItem;
  52. }
  53. return child;
  54. }
  55. return null;
  56. }
  57. /// <summary>
  58. /// 查找指定类型的父容器
  59. /// </summary>
  60. /// <typeparam name="T"></typeparam>
  61. /// <param name="child">The child.</param>
  62. /// <returns>``0.</returns>
  63. private T FindVisualParent<T>(DependencyObject child) where T : DependencyObject
  64. {
  65. if (child == null) return null;
  66. var parentObject = VisualTreeHelper.GetParent(child);
  67. if (parentObject == null) return null;
  68. var parent = parentObject as T;
  69. return parent ?? FindVisualParent<T>(parentObject);
  70. }
  71. }
  72. }