[12503] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Windows.Documents;
|
---|
| 6 | using System.Windows;
|
---|
| 7 | using System.Windows.Media;
|
---|
| 8 | using System.Windows.Input;
|
---|
| 9 | using System.Windows.Media.Effects;
|
---|
| 10 | using System.Windows.Controls;
|
---|
| 11 | using System.Windows.Shapes;
|
---|
| 12 |
|
---|
| 13 | namespace Microsoft.Research.DynamicDataDisplay.Charts
|
---|
| 14 | {
|
---|
| 15 | public class LiveToolTipAdorner : Adorner
|
---|
| 16 | {
|
---|
| 17 | private Canvas canvas = new Canvas { IsHitTestVisible = false };
|
---|
| 18 | private readonly VisualCollection visualChildren;
|
---|
| 19 | public LiveToolTipAdorner(UIElement adornedElement, LiveToolTip tooltip)
|
---|
| 20 | : base(adornedElement)
|
---|
| 21 | {
|
---|
| 22 | visualChildren = new VisualCollection(this);
|
---|
| 23 |
|
---|
| 24 | adornedElement.MouseLeave += adornedElement_MouseLeave;
|
---|
| 25 | adornedElement.MouseEnter += adornedElement_MouseEnter;
|
---|
| 26 | adornedElement.PreviewMouseMove += adornedElement_MouseMove;
|
---|
| 27 | //FrameworkElement frAdornedElement = (FrameworkElement)adornedElement;
|
---|
| 28 | //frAdornedElement.SizeChanged += frAdornedElement_SizeChanged;
|
---|
| 29 |
|
---|
| 30 | this.liveTooltip = tooltip;
|
---|
| 31 |
|
---|
| 32 | tooltip.Visibility = Visibility.Hidden;
|
---|
| 33 |
|
---|
| 34 | canvas.Children.Add(liveTooltip);
|
---|
| 35 | AddLogicalChild(canvas);
|
---|
| 36 | visualChildren.Add(canvas);
|
---|
| 37 |
|
---|
| 38 | Unloaded += LiveTooltipAdorner_Unloaded;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | //void frAdornedElement_SizeChanged(object sender, SizeChangedEventArgs e)
|
---|
| 42 | //{
|
---|
| 43 | // grid.Width = e.NewSize.Width;
|
---|
| 44 | // grid.Height = e.NewSize.Height;
|
---|
| 45 |
|
---|
| 46 | // InvalidateMeasure();
|
---|
| 47 | //}
|
---|
| 48 |
|
---|
| 49 | void LiveTooltipAdorner_Unloaded(object sender, RoutedEventArgs e)
|
---|
| 50 | {
|
---|
| 51 | canvas.Children.Remove(liveTooltip);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | void adornedElement_MouseLeave(object sender, MouseEventArgs e)
|
---|
| 55 | {
|
---|
| 56 | liveTooltip.Visibility = Visibility.Hidden;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | void adornedElement_MouseEnter(object sender, MouseEventArgs e)
|
---|
| 60 | {
|
---|
| 61 | liveTooltip.Visibility = Visibility.Visible;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | Point mousePosition;
|
---|
| 65 | private void adornedElement_MouseMove(object sender, MouseEventArgs e)
|
---|
| 66 | {
|
---|
| 67 | liveTooltip.Visibility = Visibility.Visible;
|
---|
| 68 | mousePosition = e.GetPosition(AdornedElement);
|
---|
| 69 | InvalidateMeasure();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | private void ArrangeTooltip()
|
---|
| 73 | {
|
---|
| 74 | Size tooltipSize = liveTooltip.DesiredSize;
|
---|
| 75 |
|
---|
| 76 | Point location = mousePosition;
|
---|
| 77 | location.Offset(-tooltipSize.Width / 2, -tooltipSize.Height - 1);
|
---|
| 78 |
|
---|
| 79 | liveTooltip.Arrange(new Rect(location, tooltipSize));
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | LiveToolTip liveTooltip;
|
---|
| 83 | public LiveToolTip LiveTooltip
|
---|
| 84 | {
|
---|
| 85 | get { return liveTooltip; }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | #region Overrides
|
---|
| 89 |
|
---|
| 90 | protected override Visual GetVisualChild(int index)
|
---|
| 91 | {
|
---|
| 92 | return visualChildren[index];
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | protected override int VisualChildrenCount
|
---|
| 96 | {
|
---|
| 97 | get { return visualChildren.Count; }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | protected override Size MeasureOverride(Size constraint)
|
---|
| 101 | {
|
---|
| 102 | foreach (UIElement item in visualChildren)
|
---|
| 103 | {
|
---|
| 104 | item.Measure(constraint);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | liveTooltip.Measure(constraint);
|
---|
| 108 |
|
---|
| 109 | return base.MeasureOverride(constraint);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | protected override Size ArrangeOverride(Size finalSize)
|
---|
| 113 | {
|
---|
| 114 | foreach (UIElement item in visualChildren)
|
---|
| 115 | {
|
---|
| 116 | item.Arrange(new Rect(item.DesiredSize));
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | ArrangeTooltip();
|
---|
| 120 |
|
---|
| 121 | return finalSize;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | #endregion // end of overrides
|
---|
| 125 | }
|
---|
| 126 | }
|
---|