Wpf Adorner not responding


I am trying to create an overlay in wpf (with a dimming background), similar to the ones you can find on the Internet for popup images. I would like it to be reused in more than one part of the application with different types of content.

this is a temporary adorner constructor code (just a try)

private readonly Grid _grid = new Grid(); public DarkOverlayAdorner(UIElement adornedElement, Object content) : base(adornedElement) { _grid.Background = new SolidColorBrush(Color.FromArgb(99, 0, 0, 0)); IsHitTestVisible = true; var visual = content as UIElement; if (visual != null) _grid.Children.Add(visual); } 

Also, in the class (of course), I have ovverrides MeasureOverride and ArrangeOverride to give adorner the correct size of the stolen item, GetVisualChild and VisualChildCount ...

The problem is that adorner is correctly displayed, but no events or behavior are applied to the decorated element. For instance:

 AdornerLayer layer = AdornerLayer.GetAdornerLayer(textBoxProva); layer.Add(new DarkOverlayAdorner(textBoxProva, new Button{Content = "prova"})); 

A button is displayed here, but I can’t click the button, and no effects will be applied when the mouse button is clicked. I still can not understand the problem.

+4
source share
2 answers

Well, I lost a lot of time trying to figure out what the problem is. I ended up finding a solution:

If you want the added element to respond to events, I think this element should be bound to the adorner visual tree. The way to do this is to use VisualCollection , internalized for the advertiser itself:

  VisualCollection visualChildren; FrameworkElement @object; public DarkOverlayAdorner(UIElement adornedElement) : base(adornedElement) { visualChildren = new VisualCollection(this); @object = new Button {Content = "prova"}; visualChildren.Add(@object); } protected override Visual GetVisualChild(int index) { return visualChildren[index]; } 

Thus, events are routed correctly.

+6
source

You can take a look at the ChildWindow control in Advanced WPF Tools . This is a control that produces a window with a modal background effect, and you can specify the contents to place inside the window.

0
source

Source: https://habr.com/ru/post/1333572/


All Articles