简体   繁体   English

WPF 中的 NameScope 是如何工作的?

[英]How does NameScope in WPF work?

I'm having a strange behavior with NameScopes in WPF, I have created a CustomControl called FadingPopup which is a child class of Window with nothing special inside.我在 WPF 中的 NameScopes 有一个奇怪的行为,我创建了一个名为 FadingPopup 的 CustomControl,它是 Window 的子类,内部没有什么特别的。

<Window.Resources>
    <local:FadingPopup>
        <Button Name="prec" Content="ahah"></Button>
        <Button Content="{Binding ElementName=prec, Path=Content}"></Button>
    </local:FadingPopup>
</Window.Resources>

In this snippet, the binding doesn't work (always empty).在此代码段中,绑定不起作用(始终为空)。 If I move these buttons from the resources to the content of the window like this :如果我将这些按钮从资源移动到窗口的内容,如下所示:

<Window ...>
    <Button Name="prec" Content="ahah"></Button>
    <Button Content="{Binding ElementName=prec, Path=Content}"></Button>
</Window>

The binding works as expected.绑定按预期工作。

Now, I have tried a mix between these two snippets :现在,我尝试混合使用这两个片段:

<Window...>
    <Window.Resources>
        <local:FadingPopup>
            <Button Name="prec" Content="Haha"></Button>
        </local:FadingPopup>
    </Window.Resources>
    <Button Content="{Binding ElementName=prec, Path=Content}"></Button>
</Window>

It works as well.它也有效。

Apparently, if the button prec is in the resources it registers itself in the NameScope of the Window.显然,如果按钮 prec 在资源中,它会在窗口的 NameScope 中注册自己。 BUT, it seems that the Binding tries to resolve ElementName with the NameScope of the FadingPopup (which is null), thus the binding doesn't work...但是,绑定似乎尝试使用 FadingPopup 的 NameScope(为空)解析 ElementName,因此绑定不起作用......

My first snipped works well if I specify a NameScope in my class FadingPopup :如果我在我的类 FadingPopup 中指定 NameScope ,我的第一个片段效果很好:

static FadingPopup()
{
    NameScope.NameScopeProperty.OverrideMetadata(typeof(FadingPopup), new PropertyMetadata(new NameScope()));
}

But I don't like this solution because I don't understand why, in the first snippet, prec is registered in the NameScope of Window, but ElementName is resolved with the NameScope of FadingGroup (which is null by default)...但是我不喜欢这个解决方案,因为我不明白为什么在第一个片段中,prec 是在 Window 的 NameScope 中注册的,而 ElementName 是使用 FadingGroup 的 NameScope 解析的(默认情况下为 null)...

Does someone can explain to me what is going on ?有人可以向我解释发生了什么吗? Why my first snippet doesn't work, if I don't specify a default NameScope for FadingGroup ?为什么我的第一个片段不起作用,如果我没有为 FadingGroup 指定默认的 NameScope ?

How to: Define a Name Scope | 如何:定义名称范围 Microsoft Docs provides a descent explanation on the matter. Microsoft Docs提供了对此事的后代解释。

You should check your DataContext on your control, do something like this and you should be OK.你应该检查你的控件上的 DataContext,做这样的事情,你应该没问题。 You might have to add a path, but probably not.您可能需要添加路径,但可能不需要。

<Window.Resources>
    <local:FadingPopup DataContext="{Binding}">
        <Button Name="prec" Content="ahah"></Button>
        <Button Content="{Binding ElementName=prec, Path=Content}"></Button>
    </local:FadingPopup>
</Window.Resources>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM