How to bind button Name to Content?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 22:08:42

问题


I have a list of buttons:

  <Button Style="{DynamicResource DestinationButtonStyle}">
                    <TextBlock Text="{Binding FullName}"
                               Style="{DynamicResource DestinationStationTextBlockStyle}"
                               TextTrimming="CharacterEllipsis" />
                </Button>

I want buttons to have names equal to text of content. Is it possible to make this kind of binding?


回答1:


Quote from the MSDN:

Data binding a Name is technically possible, but is an extremely uncommon scenario because a data-bound Name cannot serve the main intended purpose of the property: to provide an identifier connection point for code-behind.

In short, the Binding Name property for control is impossible and undesirable. But you can use the attached dependency property, like this:

<Button local:GiveName.Name="{Binding FullName}" ... />

Or you can use the Tag of Button, for Tag Binding works.




回答2:


You shouldn't do that. There are several restrictions for Name property. As MSDN says:

Data binding a Name is technically possible, but is an extremely uncommon scenario because a data-bound Name cannot serve the main intended purpose of the property: to provide an identifier connection point for code-behind.



来源:https://stackoverflow.com/questions/18585238/how-to-bind-button-name-to-content

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