LayoutAnchorSideControl.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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;
  15. using System.Windows;
  16. using System.Collections.ObjectModel;
  17. using Xceed.Wpf.AvalonDock.Layout;
  18. namespace Xceed.Wpf.AvalonDock.Controls
  19. {
  20. public class LayoutAnchorSideControl : Control, ILayoutControl
  21. {
  22. static LayoutAnchorSideControl()
  23. {
  24. DefaultStyleKeyProperty.OverrideMetadata(typeof(LayoutAnchorSideControl), new FrameworkPropertyMetadata(typeof(LayoutAnchorSideControl)));
  25. }
  26. internal LayoutAnchorSideControl(LayoutAnchorSide model)
  27. {
  28. if (model == null)
  29. throw new ArgumentNullException("model");
  30. _model = model;
  31. CreateChildrenViews();
  32. _model.Children.CollectionChanged += (s, e) => OnModelChildrenCollectionChanged(e);
  33. UpdateSide();
  34. }
  35. private void CreateChildrenViews()
  36. {
  37. var manager = _model.Root.Manager;
  38. foreach (var childModel in _model.Children)
  39. {
  40. _childViews.Add(manager.CreateUIElementForModel(childModel) as LayoutAnchorGroupControl);
  41. }
  42. }
  43. private void OnModelChildrenCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  44. {
  45. if (e.OldItems != null &&
  46. (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove ||
  47. e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace))
  48. {
  49. foreach (var childModel in e.OldItems)
  50. _childViews.Remove(_childViews.First(cv => cv.Model == childModel));
  51. }
  52. if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset)
  53. _childViews.Clear();
  54. if (e.NewItems != null &&
  55. (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add ||
  56. e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace))
  57. {
  58. var manager = _model.Root.Manager;
  59. int insertIndex = e.NewStartingIndex;
  60. foreach (LayoutAnchorGroup childModel in e.NewItems)
  61. {
  62. _childViews.Insert(insertIndex++, manager.CreateUIElementForModel(childModel) as LayoutAnchorGroupControl);
  63. }
  64. }
  65. }
  66. LayoutAnchorSide _model = null;
  67. public ILayoutElement Model
  68. {
  69. get { return _model; }
  70. }
  71. ObservableCollection<LayoutAnchorGroupControl> _childViews = new ObservableCollection<LayoutAnchorGroupControl>();
  72. public ObservableCollection<LayoutAnchorGroupControl> Children
  73. {
  74. get { return _childViews; }
  75. }
  76. void UpdateSide()
  77. {
  78. switch (_model.Side)
  79. {
  80. case AnchorSide.Left:
  81. SetIsLeftSide(true);
  82. break;
  83. case AnchorSide.Top:
  84. SetIsTopSide(true);
  85. break;
  86. case AnchorSide.Right:
  87. SetIsRightSide(true);
  88. break;
  89. case AnchorSide.Bottom:
  90. SetIsBottomSide(true);
  91. break;
  92. }
  93. }
  94. #region IsLeftSide
  95. /// <summary>
  96. /// IsLeftSide Read-Only Dependency Property
  97. /// </summary>
  98. private static readonly DependencyPropertyKey IsLeftSidePropertyKey
  99. = DependencyProperty.RegisterReadOnly("IsLeftSide", typeof(bool), typeof(LayoutAnchorSideControl),
  100. new FrameworkPropertyMetadata((bool)false));
  101. public static readonly DependencyProperty IsLeftSideProperty
  102. = IsLeftSidePropertyKey.DependencyProperty;
  103. /// <summary>
  104. /// Gets the IsLeftSide property. This dependency property
  105. /// indicates this control is anchored to left side.
  106. /// </summary>
  107. public bool IsLeftSide
  108. {
  109. get { return (bool)GetValue(IsLeftSideProperty); }
  110. }
  111. /// <summary>
  112. /// Provides a secure method for setting the IsLeftSide property.
  113. /// This dependency property indicates this control is anchored to left side.
  114. /// </summary>
  115. /// <param name="value">The new value for the property.</param>
  116. protected void SetIsLeftSide(bool value)
  117. {
  118. SetValue(IsLeftSidePropertyKey, value);
  119. }
  120. #endregion
  121. #region IsTopSide
  122. /// <summary>
  123. /// IsTopSide Read-Only Dependency Property
  124. /// </summary>
  125. private static readonly DependencyPropertyKey IsTopSidePropertyKey
  126. = DependencyProperty.RegisterReadOnly("IsTopSide", typeof(bool), typeof(LayoutAnchorSideControl),
  127. new FrameworkPropertyMetadata((bool)false));
  128. public static readonly DependencyProperty IsTopSideProperty
  129. = IsTopSidePropertyKey.DependencyProperty;
  130. /// <summary>
  131. /// Gets the IsTopSide property. This dependency property
  132. /// indicates this control is anchored to top side.
  133. /// </summary>
  134. public bool IsTopSide
  135. {
  136. get { return (bool)GetValue(IsTopSideProperty); }
  137. }
  138. /// <summary>
  139. /// Provides a secure method for setting the IsTopSide property.
  140. /// This dependency property indicates this control is anchored to top side.
  141. /// </summary>
  142. /// <param name="value">The new value for the property.</param>
  143. protected void SetIsTopSide(bool value)
  144. {
  145. SetValue(IsTopSidePropertyKey, value);
  146. }
  147. #endregion
  148. #region IsRightSide
  149. /// <summary>
  150. /// IsRightSide Read-Only Dependency Property
  151. /// </summary>
  152. private static readonly DependencyPropertyKey IsRightSidePropertyKey
  153. = DependencyProperty.RegisterReadOnly("IsRightSide", typeof(bool), typeof(LayoutAnchorSideControl),
  154. new FrameworkPropertyMetadata((bool)false));
  155. public static readonly DependencyProperty IsRightSideProperty
  156. = IsRightSidePropertyKey.DependencyProperty;
  157. /// <summary>
  158. /// Gets the IsRightSide property. This dependency property
  159. /// indicates this control is anchored to right side.
  160. /// </summary>
  161. public bool IsRightSide
  162. {
  163. get { return (bool)GetValue(IsRightSideProperty); }
  164. }
  165. /// <summary>
  166. /// Provides a secure method for setting the IsRightSide property.
  167. /// This dependency property indicates this control is anchored to right side.
  168. /// </summary>
  169. /// <param name="value">The new value for the property.</param>
  170. protected void SetIsRightSide(bool value)
  171. {
  172. SetValue(IsRightSidePropertyKey, value);
  173. }
  174. #endregion
  175. #region IsBottomSide
  176. /// <summary>
  177. /// IsBottomSide Read-Only Dependency Property
  178. /// </summary>
  179. private static readonly DependencyPropertyKey IsBottomSidePropertyKey
  180. = DependencyProperty.RegisterReadOnly("IsBottomSide", typeof(bool), typeof(LayoutAnchorSideControl),
  181. new FrameworkPropertyMetadata((bool)false));
  182. public static readonly DependencyProperty IsBottomSideProperty
  183. = IsBottomSidePropertyKey.DependencyProperty;
  184. /// <summary>
  185. /// Gets the IsBottomSide property. This dependency property
  186. /// indicates if this panel is anchored to bottom side.
  187. /// </summary>
  188. public bool IsBottomSide
  189. {
  190. get { return (bool)GetValue(IsBottomSideProperty); }
  191. }
  192. /// <summary>
  193. /// Provides a secure method for setting the IsBottomSide property.
  194. /// This dependency property indicates if this panel is anchored to bottom side.
  195. /// </summary>
  196. /// <param name="value">The new value for the property.</param>
  197. protected void SetIsBottomSide(bool value)
  198. {
  199. SetValue(IsBottomSidePropertyKey, value);
  200. }
  201. #endregion
  202. }
  203. }