Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Huzah! With the help of <a href="http://www.linkedin.com/pub/ghenadie-tanasiev/6/85a/532" rel="noreferrer">Ghenadie Tanasiev</a>, I've got an answer.</p> <p>Unlike most controls in WPF, adorners don't have any out-of-the-box way of assigning child elements (such as the controls I wanted to add). Without adding anything to adorners, you can only override their <code>OnRender</code> method and draw stuff within the <code>DrawingContext</code> that gets passed into it. To be honest, this fits probably 99% of use cases for adorners (stuff like creating drag handles around an object), but I needed to add some <em>proper</em> controls to my Adorner.</p> <p>The trick to doing this is to create a <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.visualcollection.aspx" rel="noreferrer"><code>VisualCollection</code></a> and set your adorner as its owner by passing it into the constructor for the collection.</p> <p>This is all described pretty comprehensively in <a href="http://www.switchonthecode.com/tutorials/wpf-tutorial-using-a-visual-collection" rel="noreferrer">this blog article</a>. Unfortunately, my repeated Google searches weren't turning this article up until I knew to search for <code>VisualCollection</code>, thanks to Ghenadie's guidance.</p> <p>This isn't mentioned in the article, but note that it is possible to combine the VisualCollection technique along with drawing in the OnRender method of the adorner. I'm using OnRender to achieve the side and top borders described in my diagram above and using VisualCollection to place and create the controls.</p> <p><strong>Edit:</strong> here is the source code from the mentioned blog post since it is no longer available:</p> <pre><code>public class AdornerContentPresenter : Adorner { private VisualCollection _Visuals; private ContentPresenter _ContentPresenter; public AdornerContentPresenter(UIElement adornedElement) : base(adornedElement) { _Visuals = new VisualCollection(this); _ContentPresenter = new ContentPresenter(); _Visuals.Add(_ContentPresenter); } public AdornerContentPresenter(UIElement adornedElement, Visual content) : this(adornedElement) { Content = content; } protected override Size MeasureOverride(Size constraint) { _ContentPresenter.Measure(constraint); return _ContentPresenter.DesiredSize; } protected override Size ArrangeOverride(Size finalSize) { _ContentPresenter.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height)); return _ContentPresenter.RenderSize; } protected override Visual GetVisualChild(int index) { return _Visuals[index]; } protected override int VisualChildrenCount { get { return _Visuals.Count; } } public object Content { get { return _ContentPresenter.Content; } set { _ContentPresenter.Content = value; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload