Binding a control's x:Name

左心房为你撑大大i 提交于 2019-12-13 16:32:16

问题


When I add

    <TextBlock  Text="{Binding SettingName}" TextWrapping="Wrap" Margin="10,-2,10,0" Style="{StaticResource PhoneTextSubtleStyle}" />    

Everuthing is ok. But when

 <TextBlock x:Name="{Binding SettingTextBlockName}"  Text="{Binding SettingName}" TextWrapping="Wrap" Margin="10,-2,10,0" Style="{StaticResource PhoneTextSubtleStyle}" />

constructor are breaking.

But I need different names in all elements.


回答1:


x:Name is a special property. As a matter of fact it's not a property at all, it's an attribute that maps the name or id property of the element to x:Name. Binding only works when applied to a DependencyProperty, so it cannot work on x:Name. It must be set manually.

If you want to distinguish between objects in runtime, you can set the Tag attribute, which tolerates everything.

more on x:Name: http://msdn.microsoft.com/en-us/library/ms752290.aspx




回答2:


You should use FrameworkElement.Tag property, according to MSDN

FrameworkElement.Tag gets or sets an arbitrary object value that can be used to store custom information about this element.

What use is the Tag property in .net



来源:https://stackoverflow.com/questions/19225437/binding-a-controls-xname

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