views:

410

answers:

1

We have a WPF application that has custom windows on a canvas which in turn contain custom controls (the main canvas containing the custom windows is again custom control displaying stuff). So basically the visual tree looks like this (without implicit Borders and other things):

- Windows
  - Canvas
    - WindowMgr
      - CustomWindow (maximized with z-index 0, functioning as background)
        - ScrollPresenter
          - CustomControl1
      - CustomWindow
        - ScrollPresenter
          - CustomControl2

Now we need drag&drop from those custom controls to each other (usually from a movable window to the background window). To show the drag&drop adorner an adorned element and an adorner layer is needed. Usually examples use their grid or itemscontrol for that, and also get the adorner layer from the same element.

Doing the same here doesn't work since the ScrollPreseneter/CustomWindows clip their content which prevents you from dragging out of the window. For now we walk up the visual tree until we find the root canvas and use that as adorned element, but that seems kind of dirty (and as we experienced isn't very robust).

Any suggestions on a robust solution for this?

A: 

If I read your question correctly and since you didn't mention it yourself you might be looking for the AdornerDecorator Class, which Provides an adorner layer for elements beneath it in the visual tree.

Assuming from its name that ScrollPresenter is derived from ContentPresenter, it's worth noting that for implementing advanced custom controls you might want to surround their ContentPresenter by an AdornerDecorator, just like the Window Class does, see for example Don’t forget the AdornerDecorator for a nice real work scenario involving drag&drop too.

That is, by means of the AdornerDecorator you'll ensure the required AdornerLayer to be contained within your custom control, hence removing the need to retrieve it elsewhere by walking up the visual tree, e.g.:

 <ControlTemplate TargetType="{x:Type CustomWindow}">
     <Border ...>
         <Grid>
             <AdornerDecorator>
                 <ScrollPresenter ... />
             </AdornerDecorator>
         </Grid>
     </Border>
 </ControlTemplate>

Depending on your particular scenario you might need an AdornerDecorator for your custom windows, your custom controls or both.

See Adorners Overview for more details on the adorner architecture.

Steffen Opel