绑定elementname到底如何工作? [英] How does binding elementname work exactly?

查看:84
本文介绍了绑定elementname到底如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得几周前曾读过它有时在模板中不起作用,并且我最近试图在两个不同的窗口中绑定内容,但找不到名称声明,因此我认为它是本地的类的名称空间,而是通过设置datacontext来绑定.但是,我真的很好奇何时可以使用绑定元素名称,何时可以使用绑定元素名称,因为在可能的情况下它要方便得多.

I remember reading a couple of weeks ago that it sometimes doesn't work inside templates, and I recently tried to bind things in two different windows and it couldn't find the name declarations, so I assumed that it was local to the namespace of the class and just bound by setting the datacontext instead. However, I'm really curious when I am able to use binding elementname and when I cannot, because it's far more convenient when it is possible.

在阅读该文章时,我发现这很有趣:

edit: In reading that article, I found this to be interesting:

因此,样式和模板都定义了自己的XAML名称范围,而与对象树中应用样式或模板的位置无关."

"For this reason, styles and templates both define their own XAML namescopes, independent of whatever location in an object tree where the style or template is applied."

如果这是真的,那不意味着Binding ElementName根本不应该在模板中工作吗?但是然后我肯定在模板中的ElementName上有一些有效的绑定.那是最令人困惑的部分,为什么有些绑定在模板内部随机起作用,而另一些则不能呢?它必须具有某种方法来尝试解析名称,即使该名称不在模板或相同的名称范围中也是如此

if this is true, doesn't that mean that Binding ElementName should not work in templates at all? But then I definitely have some working bindings on ElementName within my templates. That is the most confusing part, why do some bindings randomly work inside the templates and others do not? It must have some method for trying to resolve the name even if it isn't in the template or same namescope

推荐答案

基本上,您需要位于同一名称范围(请阅读此内容).大多数UI元素都在同一棵树中,具有相同的名称范围,但是可能会有断点和障碍(样式/模板),如果您有像DataGrid列这样的抽象对象,它们根本就没有名称范围.

Basically you need to be in the same name scope (read this). Most UI elements are in the same tree sharing the same name scope, however there can be breaks and barriers (styles/templates) and if you have abstract objects like DataGrid columns they do not have a name scope at all.

我与WPF的合作时间已经足够长,可以猜测何时会遇到问题,并且知道一些共同领域,但是并不认为有一种简单的方法可以在所有情况下预先作出判断.

I've been working with WPF long enough to guess when i'll run into problems and i know common areas but don't think there's an easy way to tell in all situations up-front.

如果这是真的,那不意味着Binding ElementName根本不应该在模板中工作吗?但是然后,我肯定在模板中的ElementName上有了一些有效的绑定.

if this is true, doesn't that mean that Binding ElementName should not work in templates at all? But then I definitely have some working bindings on ElementName within my templates.

在范围内就可以了,这是相同的作用域.这里的要点是,如果您应用模板并且它们没有自己的作用域,则会发生冲突.

Within is just fine, that is the same scope. The point here is that if you apply the template and they would not have their own scopes there would be conflicts.

例如

<Button/>
<Button/>

如果我们展开ControlTemplate,您将得到类似的内容:

If we expand the ControlTemplate you would get something like:

<Border Name="bd" Background="{TemplateBinding Background}">...</Border>
<Border Name="bd" Background="{TemplateBinding Background}">...</Border>

很显然,我们会发生名称冲突.

Obviously we would get a name conflict.

ItemsControls中的DataTemplates相同,如果您在模板中命名控件,则该名称将与其他项目的已应用模板中的同一控件实例冲突.

Same for DataTemplates in ItemsControls, if you name controls in the template that name would conflict with the same control instance in the applied template of other items.

另一方面,您可以从模板内部绑定到外部,因为在逻辑上只能有一个具有该名称的实例,或者您可以根据名称范围的接近"程度为它们指定不同的优先级,例如

On another note, you can bind from inside a template to the outside because logically there can only be one instance with that name or you can give them a distinct precedence based on how "close" the name scope is, e.g.

<TextBox Name="tb" Text="Test"/>
<ItemsControl ItemsSource="ABC">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Text, ElementName=tb}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这篇关于绑定elementname到底如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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