Alternative to ElementName in x:Bind with DataTemplates

此生再无相见时 提交于 2019-12-22 03:59:11

问题


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}

With the {x:Bind} syntax it says

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.

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

{x:Bind page.Name}

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 error WMC1110: Invalid binding path 'Page.Name' : Property 'Page' can't be found on type 'Customer'

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

Example code is available here (note specific commit)


回答1:


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.

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>

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.



来源:https://stackoverflow.com/questions/32849864/alternative-to-elementname-in-xbind-with-datatemplates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!