1: using System.Windows;
2: using System.Windows.Documents;
3: using System.Windows.Media;
4: using System.Windows.Controls;
5:
6: namespace AdornerTest1
7: {
8: public class PopUp : Adorner
9: {
10: private VisualCollection _Visuals;
11: private ContentPresenter _ContentPresenter;
12:
13: /// <summary>
14: /// Initializes a new instance of the <see cref="PopUp"/> class.
15: /// </summary>
16: /// <param name="adornedElement">The element to bind the adorner to.</param>
17: /// <exception cref="T:System.ArgumentNullException">Raised when adornedElement is null.</exception>
18: public PopUp(UIElement adornedElement)
19: : base(adornedElement)
20: {
21: _Visuals = new VisualCollection(this);
22: _ContentPresenter = new ContentPresenter();
23: _Visuals.Add(_ContentPresenter);
24: }
25:
26: /// <summary>
27: /// Initializes a new instance of the <see cref="PopUp"/> class.
28: /// </summary>
29: /// <param name="adornedElement">The adorned element.</param>
30: /// <param name="content">The content.</param>
31: public PopUp(UIElement adornedElement, Visual content)
32: : this(adornedElement)
33: { Content = content; }
34:
35: /// <summary>
36: /// Implements any custom measuring behavior for the adorner.
37: /// </summary>
38: /// <param name="constraint">A size to constrain the adorner to.</param>
39: /// <returns>
40: /// A <see cref="T:System.Windows.Size"/> object representing the amount of layout space needed by the adorner.
41: /// </returns>
42: protected override Size MeasureOverride(Size constraint)
43: {
44: _ContentPresenter.Measure(constraint);
45: return _ContentPresenter.DesiredSize;
46: }
47:
48: /// <summary>
49: /// When overridden in a derived class, positions child elements and determines a size for a <see cref="T:System.Windows.FrameworkElement"/> derived class.
50: /// </summary>
51: /// <param name="finalSize">The final area within the parent that this element should use to arrange itself and its children.</param>
52: /// <returns>The actual size used.</returns>
53: protected override Size ArrangeOverride(Size finalSize)
54: {
55: _ContentPresenter.Arrange(new Rect(0, 0,
56: finalSize.Width, finalSize.Height));
57: return _ContentPresenter.RenderSize;
58: }
59:
60: /// <summary>
61: /// Overrides <see cref="M:System.Windows.Media.Visual.GetVisualChild(System.Int32)"/>, and returns a child at the specified index from a collection of child elements.
62: /// </summary>
63: /// <param name="index">The zero-based index of the requested child element in the collection.</param>
64: /// <returns>
65: /// The requested child element. This should not return null; if the provided index is out of range, an exception is thrown.
66: /// </returns>
67: protected override Visual GetVisualChild(int index)
68: { return _Visuals[index]; }
69:
70: protected override int VisualChildrenCount
71: { get { return _Visuals.Count; } }
72:
73: /// <summary>
74: /// Gets or sets the content.
75: /// </summary>
76: /// <value>The content.</value>
77: public object Content
78: {
79: get { return _ContentPresenter.Content; }
80: set { _ContentPresenter.Content = value; }
81: }
82: }
83: }