سؤال

I have a Grid with a Adorner to provide some drawn pattern. See img: http://imgur.com/D649W

My problem is that this Adorner(dots on the Grid) is layered on top of everything. The white square are draggable but now when the Adorner are on top, I can't drag. I would like the layer to be behind every component added to the Grid. Any suggestions on how I can set the ZIndex?

Thanks.

Code below:

  MyAdorner ad = new MyAdorner(grid);
  AdornerLayer adLayer = AdornerLayer.GetAdornerLayer(grid);
  adLayer.Add(ad);

I push my Button and this is adding the MyAdorner to the grid. MyAdorner looks like this:

public MyAdorner(Grid adornedGrid)
: base(adornedGrid) {
Height = adornedGrid.Height;
Width = adornedGrid.Width;
brush = new VisualBrush();
brush.Stretch = Stretch.Fill;
brush.TileMode = TileMode.Tile;
brush.Viewport = new Rect(0, 0, SnapDistance, SnapDistance);
brush.ViewportUnits = BrushMappingMode.Absolute;
brush.Viewbox = new Rect(0, 0, SnapDistance, SnapDistance);
brush.ViewboxUnits = BrushMappingMode.Absolute;
ellipse = new Ellipse() { Fill = new SolidColorBrush(Colors.Blue), Width = 2, Height = 2 };
brush.Visual = ellipse;
}

protected override void OnRender(System.Windows.Media.DrawingContext drawingContext) { 
Pen renderPen = new Pen(new SolidColorBrush(Colors.Black), 0); 
drawingContext.DrawRectangle(brush, renderPen, new Rect(new Point(0, 0), AdornedElement.DesiredSize)); 
}
هل كانت مفيدة؟

المحلول

Is this what you're looking for?

Panel.SetZIndex(ad, 20)

Attached properties of the framework are usually asignable from static methods of the UIElement that holds it.

EDIT:
Possible alternative: - make your own Panel

نصائح أخرى

If your problem is that the adorner is covering the elements you want to manipulate so that they become un-draggable etc, set .IsHitTestVisible = False on the adorner.

You can also set the adorner's opacity to some semi-transparent value to see the background through it if that is desirable.

Easy and dirty way to make sure that your wanted elements are ALWAYS on top:

Declare a static in a Util library:

      public static int ZIndexCount;

Then when you want an element on top you simply do:

         SetZIndex(_viewbox, Util.ZIndexCount++);

Of course, if your application runs 5 years without being interrupted the ZIndexCount will go back to 0...

It works like a charm in my applications.

I know this is quite old but I thought I try anyway: You can add a new AdornerDecorator to you visual tree hierarchy to render the controls at the right level. By default the root of the tree provides the AdornerDecorator but you can add as many as you want and your the components you add will be rendered in them. For more information - see here

  <Grid>
     <AdornerDecorator>
        ...your Adorners render here
     </AdornerDecorator>
 </Grid>

https://wangmo.wordpress.com/2008/10/19/relations-between-adorner-adornerlayer-and-adornerdecorator/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top