AdornerLayer.GetAdornerLayer()返回NULL的面板中的所有控件 [英] AdornerLayer.GetAdornerLayer() return NULL for all controls in a Panel

查看:2009
本文介绍了AdornerLayer.GetAdornerLayer()返回NULL的面板中的所有控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面对的事实,我不能很好地理解AdornerLayer如何添加了UI元素

I'm facing the fact that i cannot understand Well how AdornerLayer is added for UIElements.

我有这样的情况:

我有它内置有3控件的WPF形式:
A 电网上这是1 按钮 1 文本框

I have a WPF Form which is built with 3 controls: A Grid on which are 1 Button and 1 TextBox.

在我的系统,当我点击打开这个表,所有3个元素都具有 AdornerLayer不为空

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



集合的 controlsWithAddorner 包含我所有的3控制。

The collection controlsWithAddorner contains all my 3 controls.

的方法 GetUIElements(FrameworkElement的父母)返回的IEnumerable< FrameworkElement的方式> 在其中内的所有控件的小组

The method GetUIElements(FrameworkElement parent) returns an IEnumerable<FrameworkElement> in which are all controls within a Panel.

我有这样一个功能:
刷新窗体设计器即可。这对于重新创建,形成了XAML。

I have such a functionality: Refresh Form Designer. Which recreates the xaml for that Form.

之后的刷新完成后,我检查为AdornerLayer控件列表。对于所有控制AdornerLayer为空。

After that Refresh is done, I check the list of controls for AdornerLayer. For all controls the AdornerLayer is null.

问题就在这里,我能不明白的地方AdornerLayer(S)都失去了什么?
我应该小心添加它们FORR每次的UIElement当我刷新表单的设计?

The problem is here, I cannot understand where AdornerLayer (s) are lost? Should I take care To add them forr each UIElement when I Refresh the Designer of the Form?

请与一些建议咨询我。

Please advice me with some suggestions.

编辑:<! / STRONG>
我会告诉所有的解决方案,如果对方会遇到这样的问题:)

I'll show all the solution if other will encounter such problems :)

的任务是:当在设计师SelectedControl,保持它选用甚至RefreshDesigner完成。

The mission is: When there is a SelectedControl in designer, keep it selected even a RefreshDesigner is done.

RefreshDesigner 功能再现整个形式的XAML。

RefreshDesigner functionality recreates the xaml for the whole 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();
}



首先:窗体的XAML是更新通过使用 ParseXaml()方法从 XamlProvider

Firstly: The xaml of the Form is Updated by 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;
}



第二: 内容.UpdateLayout();
确保此元素的所有可视子元素都正确进行了布局更新。
MSDN官方消息

在此之后,所有的元素都AdornelLayer不为空,我能够设置装饰器边界为设计师preciosly选择的控制。

After that, All elements have AdornelLayer not Null and I'm able to set the Adorner Border for preciosly selected control in designer.

推荐答案

窗体后刷新,调用 Content.UpdateLayout(); 来确保内容的所有可视子元素进行适当更新了布局。 MSDN官方

After the Form is Refreshed, call Content.UpdateLayout(); to Ensures that all visual child elements of the Content were properly updated for layout. MSDN official

这篇关于AdornerLayer.GetAdornerLayer()返回NULL的面板中的所有控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆