AdornerLayer.GetAdornerLayer () returns NULL for all controls in the panel

I have to deal with the fact that I can’t understand how much AdornerLayer has been added for UIElements.

I have this situation:

I have a WPF form that is built with three controls: A Grid on which there is 1 Button and 1 Text Box . enter image description here

On my system, when I click to open this form, all 3 elements have AdornerLayer not null .

var controls = _frameworkElementProvider.GetUIElements(Content); var controlsWithAddorner = new List<FrameworkElement>(); foreach (var control in controls) { var adornerLayer = AdornerLayer.GetAdornerLayer(control); if (adornerLayer != null) { controlsWithAddorner.Add(control); } } 

The controlsWithAddorner collection contains all of my 3 controls.

The GetUIElements(FrameworkElement parent) method returns IEnumerable<FrameworkElement> , which contains all the controls in the panel .

I have this functionality: Update the form constructor . What recreates haml for this Form.

After this Update , I check the list of controls for AdornerLayer. For all controls, AdornerLayer is NULL.

The problem is here, I can’t understand where the AdornerLayer is lost? Do I have to take care to add them for each UIElement when I update the form constructor?

Please advise me some suggestions.

Thanks!


EDIT: I will show the whole solution if others encounter such problems :)

The mission is this: when there is a SelectedControl constructor in the designer, save it even if RefreshDesigner is running.

enter image description here

The RefreshDesigner function recreates xaml for the entire form.

 // Refresh the Designer private void RefreshDesigner() { Content = _xamlProvider.ParseXaml(_xaml.ToString()); //Here was the Problem. All visual child elements of the Content wa not updated after xaml recreation. //By including that call -> solved the problem Content.UpdateLayout(); } 

First : the xaml of the form is updated using the ParseXaml() method from XamlProvider

 // in XamlProvider class public Panel ParseXaml(string xaml) { var regex = new Regex("<Grid "); const int first = 1; xaml = Regex.Replace(xaml, @"xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""", string.Empty); xaml = Regex.Replace(xaml, @"xml:space=""preserve""", string.Empty); //... xaml = Regex.Replace(xaml, "<BindingGroup .*/>", string.Empty); var content = (Panel)XamlReader.Parse(xaml); return content; } 

Secondly: Content.UpdateLayout(); Ensures that all visual child elements of this element are correctly updated for the layout. Official MSDN Source

After that, all elements have AdornelLayer not Null, and I can set Adorner Border for the pre-selected control in the designer.

+3
source share
1 answer

After the form is updated, call Content.UpdateLayout(); to ensure that all visual children of the Content are correctly updated for the layout. Official MSDN

+3
source

All Articles