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