Search code examples
c#wpfgridadorner

C# WPF - Adorner ZIndex


I have a Grid with a Adorner to provide some drawn pattern. See img: https://i.sstatic.net/HUJmh.jpg

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)); 
}

Solution

  • 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