替代的ElementName在X:用的DataTemplates绑定 [英] Alternative to ElementName in x:Bind with DataTemplates

查看:199
本文介绍了替代的ElementName在X:用的DataTemplates绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用传统的{结合}语法,你可以指定元素名称指向页面上的特定控制,并能够访问它的属性。例如,如果页面被命名为你可以这样做:

When using traditional {Binding} syntax you could specify element name to point to a specific control on the page, and be able to access its properties. For example if the page is named page you could do:

{Binding ElementName=Page, Path=Name}

随着 {X:}绑定语法它说:

使用X:绑定,你并不需要使用的ElementName = XXX是部分
  结合前pression。随着X:绑定,您可以使用元素的名称
  作为路径的对结合因为命名的元素的第一部分
  成为页面或用户控件,再presents根中的字段
  绑定源。

With x:Bind, you do not need to use ElementName=xxx as part of the binding expression. With x:Bind, you can use the name of the element as the first part of the path for the binding because named elements become fields within the page or user control that represents the root binding source.

因此​​,对于上面的例子{X:}绑定将

So for the example above in {x:Bind} would be

{x:Bind page.Name}

这工作得很好,直到它是一个数据模板(例如一个ListView的ItemTemplate)中。在这种情况下,不再工作,因为它是寻找上指定的数据类型,从而导致了以下错误(假设我的数据类型客户

Which works fine, until it is inside a data template (for example a ListView's ItemTemplate). In which case it no longer works as it is looking for Page on the data type specified which leads to the following error (assuming my data type is customer):

XamlCompiler错误WMC1110:无效的绑定路径Page.Name:
  属性页无法在类型中找到客户

XamlCompiler error WMC1110: Invalid binding path 'Page.Name' : Property 'Page' can't be found on type 'Customer'

什么是解决方案中使用{X:}绑定?语法的DataTemplates和访问控制数据模板外

What is the solution to use {x:Bind} syntax with datatemplates and access controls outside the data template?

举例code可这里(注意具体落实)

Example code is available here (note specific commit)

推荐答案

据我所知,在这个时间点有没有办法使用x直接绑定到一个控件的属性:绑定方法,因为它不支持其绑定定义内的元素名称。

As far as I know at this point in time there is no way to directly bind to a property of a control using the x:bind method as it does not support the element name inside of its binding definition.

这并不意味着你不能绑定到一个DataTemplate内控制你仍然可以做这样的事情,以访问控制,但你只是不能够使用编译绑定,X:绑定语法

That does not mean you cant bind to a control inside a dataTemplate you can still do something like this to access controls but you just aren't able to use the compiled binding x:Bind syntax.

 <DataTemplate x:DataType="local:Customer">
     <StackPanel Orientation="Vertical">
         <Button Content="{Binding Name, ElementName=page}" />
         <TextBlock Text="{x:Bind Title}" />
     </StackPanel>        
 </DataTemplate>

原因你所得到的错误是由于这样的数据模板父的数据源。在X:绑定绑定不能引用控制目标和您的客户类型做Page.Name财产或路径。正如上文访问您的控制范围之外的用户控件属性的唯一途径只能显示使用XAML是诉诸回标准约束机制。

The reason for the error you are getting is due to the way data templates parent their datasource. The x:Bind binding cannot reference a control object and your Customer type does Page.Name property or path. As shown above the only real way of accessing user control properties outside of your control only using XAML is to resort back to the standard binding mechanism.

我希望这回答了你的问题。

I hope this answers your question.

这篇关于替代的ElementName在X:用的DataTemplates绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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