PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Common/WidthSpring.cs

#
C# | 97 lines | 73 code | 14 blank | 10 comment | 8 complexity | 486b9f4f0626f5b501a4448a2d8cba31 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. namespace Microsoft.Research.DynamicDataDisplay.Charts
  8. {
  9. /// <summary>
  10. /// Represents a kind of 'spring' that makes width of one plotter's LeftPanel equal to other plotter's LeftPanel.
  11. /// </summary>
  12. public class WidthSpring : FrameworkElement, IPlotterElement
  13. {
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="WidthSpring"/> class.
  16. /// </summary>
  17. public WidthSpring()
  18. {
  19. }
  20. #region Properties
  21. /// <summary>
  22. /// Gets or sets panel which is a source of width.
  23. /// </summary>
  24. /// <value>The source panel.</value>
  25. public Panel SourcePanel
  26. {
  27. get { return (Panel)GetValue(SourcePanelProperty); }
  28. set { SetValue(SourcePanelProperty, value); }
  29. }
  30. public static readonly DependencyProperty SourcePanelProperty = DependencyProperty.Register(
  31. "SourcePanel",
  32. typeof(Panel),
  33. typeof(WidthSpring),
  34. new FrameworkPropertyMetadata(null, OnSourcePanelReplaced));
  35. private static void OnSourcePanelReplaced(DependencyObject d, DependencyPropertyChangedEventArgs e)
  36. {
  37. WidthSpring owner = (WidthSpring)d;
  38. owner.OnSourcePanelReplaced((Panel)e.OldValue, (Panel)e.NewValue);
  39. }
  40. private void OnSourcePanelReplaced(Panel prevPanel, Panel currPanel)
  41. {
  42. if (prevPanel != null)
  43. {
  44. prevPanel.SizeChanged -= OnPanel_SizeChanged;
  45. }
  46. if (currPanel != null)
  47. {
  48. currPanel.SizeChanged += OnPanel_SizeChanged;
  49. }
  50. UpdateWidth();
  51. }
  52. void OnPanel_SizeChanged(object sender, SizeChangedEventArgs e)
  53. {
  54. UpdateWidth();
  55. }
  56. private void UpdateWidth()
  57. {
  58. Panel parentPanel = Parent as Panel;
  59. if (parentPanel != null && SourcePanel != null)
  60. {
  61. Width = Math.Max(SourcePanel.ActualWidth - (parentPanel.ActualWidth - ActualWidth), 0);
  62. }
  63. }
  64. #endregion // end of Properties
  65. #region IPlotterElement Members
  66. private Plotter plotter;
  67. public void OnPlotterAttached(Plotter plotter)
  68. {
  69. this.plotter = plotter;
  70. plotter.LeftPanel.Children.Insert(0, this);
  71. }
  72. public void OnPlotterDetaching(Plotter plotter)
  73. {
  74. plotter.LeftPanel.Children.Remove(this);
  75. this.plotter = null;
  76. }
  77. public Plotter Plotter
  78. {
  79. get { return plotter; }
  80. }
  81. #endregion
  82. }
  83. }