LayoutAnchorGroupControl.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.Collections.ObjectModel;
  16. using Xceed.Wpf.AvalonDock.Layout;
  17. using System.Windows;
  18. namespace Xceed.Wpf.AvalonDock.Controls
  19. {
  20. public class LayoutAnchorGroupControl : Control, ILayoutControl
  21. {
  22. static LayoutAnchorGroupControl()
  23. {
  24. DefaultStyleKeyProperty.OverrideMetadata(typeof(LayoutAnchorGroupControl), new FrameworkPropertyMetadata(typeof(LayoutAnchorGroupControl)));
  25. }
  26. internal LayoutAnchorGroupControl(LayoutAnchorGroup model)
  27. {
  28. _model = model;
  29. CreateChildrenViews();
  30. _model.Children.CollectionChanged += (s, e) => OnModelChildrenCollectionChanged(e);
  31. }
  32. private void CreateChildrenViews()
  33. {
  34. var manager = _model.Root.Manager;
  35. foreach (var childModel in _model.Children)
  36. {
  37. _childViews.Add(new LayoutAnchorControl(childModel) { Template = manager.AnchorTemplate });
  38. }
  39. }
  40. private void OnModelChildrenCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  41. {
  42. if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove ||
  43. e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace)
  44. {
  45. if (e.OldItems != null)
  46. {
  47. {
  48. foreach (var childModel in e.OldItems)
  49. _childViews.Remove(_childViews.First(cv => cv.Model == childModel));
  50. }
  51. }
  52. }
  53. if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset)
  54. _childViews.Clear();
  55. if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add ||
  56. e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace)
  57. {
  58. if (e.NewItems != null)
  59. {
  60. var manager = _model.Root.Manager;
  61. int insertIndex = e.NewStartingIndex;
  62. foreach (LayoutAnchorable childModel in e.NewItems)
  63. {
  64. _childViews.Insert(insertIndex++, new LayoutAnchorControl(childModel) { Template = manager.AnchorTemplate });
  65. }
  66. }
  67. }
  68. }
  69. ObservableCollection<LayoutAnchorControl> _childViews = new ObservableCollection<LayoutAnchorControl>();
  70. public ObservableCollection<LayoutAnchorControl> Children
  71. {
  72. get { return _childViews; }
  73. }
  74. LayoutAnchorGroup _model;
  75. public ILayoutElement Model
  76. {
  77. get { return _model; }
  78. }
  79. }
  80. }