DropDownButton.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*************************************************************************************
  2. Extended WPF Toolkit
  3. Copyright (C) 2007-2013 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features, controls, and fast professional support,
  7. pick up the Plus Edition at http://xceed.com/wpf_toolkit
  8. Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
  9. ***********************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Windows.Controls.Primitives;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Data;
  18. using System.Diagnostics;
  19. namespace Xceed.Wpf.AvalonDock.Controls
  20. {
  21. public class DropDownButton : ToggleButton
  22. {
  23. public DropDownButton()
  24. {
  25. this.Unloaded += new RoutedEventHandler(DropDownButton_Unloaded);
  26. }
  27. #region DropDownContextMenu
  28. /// <summary>
  29. /// DropDownContextMenu Dependency Property
  30. /// </summary>
  31. public static readonly DependencyProperty DropDownContextMenuProperty =
  32. DependencyProperty.Register("DropDownContextMenu", typeof(ContextMenu), typeof(DropDownButton),
  33. new FrameworkPropertyMetadata((ContextMenu)null,
  34. new PropertyChangedCallback(OnDropDownContextMenuChanged)));
  35. /// <summary>
  36. /// Gets or sets the DropDownContextMenu property. This dependency property
  37. /// indicates drop down menu to show up when user click on an anchorable menu pin.
  38. /// </summary>
  39. public ContextMenu DropDownContextMenu
  40. {
  41. get { return (ContextMenu)GetValue(DropDownContextMenuProperty); }
  42. set { SetValue(DropDownContextMenuProperty, value); }
  43. }
  44. /// <summary>
  45. /// Handles changes to the DropDownContextMenu property.
  46. /// </summary>
  47. private static void OnDropDownContextMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  48. {
  49. ((DropDownButton)d).OnDropDownContextMenuChanged(e);
  50. }
  51. /// <summary>
  52. /// Provides derived classes an opportunity to handle changes to the DropDownContextMenu property.
  53. /// </summary>
  54. protected virtual void OnDropDownContextMenuChanged(DependencyPropertyChangedEventArgs e)
  55. {
  56. var oldContextMenu = e.OldValue as ContextMenu;
  57. if (oldContextMenu != null && IsChecked.GetValueOrDefault())
  58. oldContextMenu.Closed -= new RoutedEventHandler(OnContextMenuClosed);
  59. }
  60. #endregion
  61. #region DropDownContextMenuDataContext
  62. /// <summary>
  63. /// DropDownContextMenuDataContext Dependency Property
  64. /// </summary>
  65. public static readonly DependencyProperty DropDownContextMenuDataContextProperty =
  66. DependencyProperty.Register("DropDownContextMenuDataContext", typeof(object), typeof(DropDownButton),
  67. new FrameworkPropertyMetadata((object)null));
  68. /// <summary>
  69. /// Gets or sets the DropDownContextMenuDataContext property. This dependency property
  70. /// indicates data context to set for drop down context menu.
  71. /// </summary>
  72. public object DropDownContextMenuDataContext
  73. {
  74. get { return (object)GetValue(DropDownContextMenuDataContextProperty); }
  75. set { SetValue(DropDownContextMenuDataContextProperty, value); }
  76. }
  77. #endregion
  78. protected override void OnClick()
  79. {
  80. if (DropDownContextMenu != null)
  81. {
  82. //IsChecked = true;
  83. DropDownContextMenu.PlacementTarget = this;
  84. DropDownContextMenu.Placement = PlacementMode.Bottom;
  85. DropDownContextMenu.DataContext = DropDownContextMenuDataContext;
  86. DropDownContextMenu.Closed += new RoutedEventHandler(OnContextMenuClosed);
  87. DropDownContextMenu.IsOpen = true;
  88. }
  89. base.OnClick();
  90. }
  91. void OnContextMenuClosed(object sender, RoutedEventArgs e)
  92. {
  93. //Debug.Assert(IsChecked.GetValueOrDefault());
  94. var ctxMenu = sender as ContextMenu;
  95. ctxMenu.Closed -= new RoutedEventHandler(OnContextMenuClosed);
  96. IsChecked = false;
  97. }
  98. void DropDownButton_Unloaded(object sender, RoutedEventArgs e)
  99. {
  100. // When changing theme, Unloaded event is called, erasing the DropDownContextMenu.
  101. // Prevent this on theme changes.
  102. if( this.IsLoaded )
  103. {
  104. DropDownContextMenu = null;
  105. }
  106. }
  107. }
  108. }